$1には<td>、$3は</td>が配置され、$(this).children('td').data('link')が、
http//jquery.com/とhttp://jqueryui.comになるということは理解出来ています。
$2には正規表現([^<]+)に該当するjQueryとjQuery UIの文字列が入るのですが、
パターンの意味自体がよく分かりません。
これは<を含まない文字列をキャプチャするという意味合いでしょうか。
このパターンを用いる理由もよく分からないです。
よろしくお願い致します。
HTML
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <title></title> 6</head> 7<body> 8 <table> 9 <tr></tr> 10 <tr></tr> 11 </table> 12<script src="js/jquery-3.6.0.min.js"></script> 13<script src="js/app.js"></script> 14</body> 15</html>
JavaScript
1// 改訂新版jQuery本格入門 P126 リスト3-13より 2 3// それぞれの<tr>に<td>jQuery</td>と<td>jQuery UI</td>を追加 4$('<td>', { text : 'jQuery', data : { link : 'http//jquery.com/' }}) 5 .appendTo('tr:first'); 6$('<td>', { text : 'jQuery UI', data : { link : 'http://jqueryui.com' }}) 7 .appendTo('tr:last'); 8 9// それぞれの<tr>の最後の子要素に<td>を追加 10$('table tr').append(function(index, html) { 11 return html.replace(/(<td>)([^<]+)(<\/td>)/, 12 "$1<a href=\"" + $(this).children('td').data('link') + "\">$2</a>$3"); 13}); 14 15// それぞれの<tr>の最初の子要素に<td>を追加 16$('table tr').prepend(function(index, html) { 17 return '<td>' + (index + 1) + '</td>'; 18});
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/09/06 13:54