Jquery 使用td:contains仅查找精确匹配
我知道这已经被问了好几次了,但对我来说不起作用。我有这个:Jquery 使用td:contains仅查找精确匹配,jquery,selector,contains,Jquery,Selector,Contains,我知道这已经被问了好几次了,但对我来说不起作用。我有这个: $("td:contains('Hello')").html("Hi"); $("td:contains('Hello World')").html("Bye"); 我已经做到了: $("td:contains('Hello')").filter(function() { return $(this).text() == "Hi"; }); 但两人都会说“嗨”。我只希望将具有确切字符串
$("td:contains('Hello')").html("Hi");
$("td:contains('Hello World')").html("Bye");
我已经做到了:
$("td:contains('Hello')").filter(function() {
return $(this).text() == "Hi";
});
但两人都会说“嗨”。我只希望将具有确切字符串“Hello”的表数据替换为“Hi”。“Hello World”应该替换为“Bye”,但事实并非如此。有人能帮忙吗?你似乎想要这个:
$("td").filter(function() {
return $(this).text() == "Hello";
}).text('Hi');
contains
是子字符串匹配项。您执行的第一个操作将替换任何包含Hello
的节点,因此Hello World
将被销毁。第二行将不匹配任何内容,因为文档中不再有Hello World
节点
如果您只是简单地颠倒操作顺序:
$("td:contains('Hello World')").html("Bye");
$("td:contains('Hello')").html("Hi");
然后它会像预期的那样工作首先运行更具体的命令(即Hello World),然后运行不太具体的contains。这个命令有效。我知道事情就这么简单。非常感谢你。