是否有一个不区分大小写的jQuery:包含选择器? [英] Is there a case insensitive jQuery :contains selector?
问题描述
:包含 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扩展为:包含不区分大小写的选择器:包含选择器保持不变。
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 的 question )
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:包含选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!