[英]change table cell value 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!');
});
使用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";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.