// NEW selector
jQuery.expr[':'].Contains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
// OVERWRITES old selecor
jQuery.expr[':'].contains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
更新以适用于jQuery 1.8
$.expr[":"].contains = $.expr.createPseudo(function(arg) {
return function( elem ) {
return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
};
});
有了这个,
$("div:contains('John')")
将选择所有这三个元素:
<div>john</div>
<div>John</div>
<div>hey hey JOHN hey hey</div>
通过Pablo Fortes进行演示 。
翻译自: https://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/