特定のテキスト値を含む表のセルを見つけて、それを別のものに変更する必要があります。
<table><tr>
<td>You are nice</td>
<td>I hate you</td>
</tr></table>
「嫌い」を含む表のセルを見つけて、「愛してる」に変更します。
Jqueryでそれを行うにはどうすればよいですか?
特定のテキスト値を含む表のセルを見つけて、それを別のものに変更する必要があります。
<table><tr>
<td>You are nice</td>
<td>I hate you</td>
</tr></table>
「嫌い」を含む表のセルを見つけて、「愛してる」に変更します。
Jqueryでそれを行うにはどうすればよいですか?
:contains
セレクターの使用:
$('td:contains("I hate you")').text('....');
使用filter
方法:
$('td').filter(function(){
// contains
return $(this).text().indexOf("I hate you") > -1;
// exact match
// return $(this).text() === "I hate you";
}).text('...');
または:
$('td').text(function(i, text){
return text.replace('I hate you', 'I love you!');
});
単純なcontains
セレクターでトリックを実行し、その後にテキスト値を設定する必要があります
$("td:contains('I hate you')").text('I love you');
querySelectorAll("td") を使用し、返されたすべての要素を反復処理して、textNode の値を確認します。
var tds = document.querySelectorAll("td");
for (var i = 0; i < tds.length; i++) {
if (tds[i].firstChild.nodeValue == "I hate you"){
tds[i].firstChild.nodeValue = "I love you";
}
}