是否有不区分大小写的 jQuery :contains 选择器? [英] Is there a case insensitive jQuery :contains selector?
问题描述
是否有 :contains jQuery 选择器的不区分大小写版本,或者我应该做这个工作手动通过循环所有元素并将它们的 .text() 与我的字符串进行比较?
Is there a case insensitive version of the :contains jQuery selector or should I do the work manually by looping over all elements and comparing their .text() to my string?
推荐答案
我最终为 jQuery 1.2 做的是:
What I ended up doing for jQuery 1.2 is :
jQuery.extend(
jQuery.expr[':'], {
Contains : "jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0"
});
这将扩展 jquery 以使其具有不区分大小写的 :Contains 选择器,而 :contains 选择器保持不变.
This will extend jquery to have a :Contains selector that is case insensitive, the :contains selector remains unchanged.
对于 jQuery 1.3(感谢@user95227)及更高版本,您需要
For jQuery 1.3 (thanks @user95227) and later you need
jQuery.expr[':'].Contains = function(a,i,m){
return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};
显然使用
(a.textContent || a.innerText || "")
代替
jQuery(a).text()
在前面的表达式中,它大大加快了速度,所以如果速度是一个问题,请自担风险.(参见 @John 的 问题)
In the previous expression speeds it up considerably so try at your own risk if speed is an issue. (see @John 's question)
最新对于 jQuery 1.8,它应该是:
Latest edit: For jQuery 1.8 it should be:
jQuery.expr[":"].Contains = jQuery.expr.createPseudo(function(arg) {
return function( elem ) {
return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
};
});
这篇关于是否有不区分大小写的 jQuery :contains 选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!