我正在尝试搜索并替换同一个单词的所有实例,使用 .contains () 不区分大小写,但它似乎不起作用并且区分大小写。这是我现在拥有的代码:
<p>some text</p>
<p>Some Text</p>
<p>Some TEXT</p>
jQuery.expr[':'].Contains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
jQuery.expr[':'].contains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
$('p').filter(":contains('some text')").each(function(){
$(this).text($(this).text().replace("some text", "replace with new text"));
});
这只会更改第一个文本,因为同样的情况,您可以在此处查看 js fiddle 上的示例 http://jsfiddle.net/ka82V/