更改表格单元格值jquery
我需要找到一个包含某些文本值的表格单元格,并将其更改为其他内容。
You are nice I hate you
找到包含“我讨厌你”的表格单元格并将其更改为“我爱你”。
我如何在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"; } }