如何使 jQuery 包含不区分大小写,包括 jQuery 1.8+?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2196641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:58:56  来源:igfitidea点击:

How do I make jQuery Contains case insensitive, including jQuery 1.8+?

jquerycontainscase-sensitivecase-insensitive

提问by Matrym

I'm trying to use "contains" case insensitively. I tried using the solution at the following stackoverflow question, but it didn't work:

我试图不敏感地使用“包含”大小写。我尝试在以下 stackoverflow 问题中使用该解决方案,但没有奏效:

Is there a case insensitive jQuery :contains selector?

是否有不区分大小写的 jQuery :contains 选择器?

For convenience, the solution is copied here:

为方便起见,这里复制解决方案:

jQuery.extend(
        jQuery.expr[':'], { 
                Contains : "jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0" 
});

Here is the error:

这是错误:

Error: q is not a function
Source File: /js/jquery-1.4.js?ver=1.4
Line: 81

Here's where I'm using it:

这是我使用它的地方:

  $('input.preset').keyup(function() {
    $(this).next().find("li").removeClass("bold");
    var theMatch = $(this).val();
    if (theMatch.length > 1){
      theMatch = "li:Contains('" + theMatch + "')";
      $(this).next().find(theMatch).addClass("bold");
    }
  });

My use of the original case sensitive "contains" in the same scenario works without any errors. Does anyone have any ideas? I'd appreciate it.

我在同一场景中使用原始区分大小写的“包含”没有任何错误。有没有人有任何想法?我会很感激。

回答by Nick Craver

This is what i'm using in a current project, haven't had any problems. See if you have better luck with this format:

这是我在当前项目中使用的,没有任何问题。看看你是否对这种格式有更好的运气:

jQuery.expr[':'].Contains = function(a, i, m) { 
  return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0; 
};


In jQuery 1.8 the API for this changed, the jQuery 1.8+ version of this would be:

在 jQuery 1.8 中,此 API 发生了变化,jQuery 1.8+ 版本将是:

jQuery.expr[":"].Contains = jQuery.expr.createPseudo(function(arg) {
    return function( elem ) {
        return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});

You can test it out here. For more detail on 1.8+ custom selectors, check out the Sizzle wiki here.

你可以在这里测试一下。有关 1.8+ 自定义选择器的更多详细信息,请在此处查看 Sizzle wiki

回答by Ellipsis

It's worth noting that the answer is correct but only covers :Contains, and not the alias :containswhich could lead to unexpected behavior (or could be used by design for advanced applications that require both sensitive and insensitive search).

值得注意的是,答案是正确的,但仅涵盖:Contains,而不是:contains可能导致意外行为的别名(或者可以通过设计用于需要敏感和不敏感搜索的高级应用程序)。

This could be resolved by duplicating the extention for the alias:

这可以通过复制别名的扩展来解决:

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; 
};

Took me a while to work out why it wasn't working for me.

我花了一段时间才弄清楚为什么它对我不起作用。

回答by Mina Gabriel

I would do something like this

我会做这样的事情

     $.expr[':'].containsIgnoreCase = function (n, i, m) {
        return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
    };

And Leave :containsAlone...

并留下一个:contains人...

DEMO

演示

So why jQuery doesn't support it in it's library?! if it is that easy...

那么为什么 jQuery 在它的库中不支持它呢?!如果有那么容易...

because Does your code pass the turkey code?

因为 您的代码是否通过了土耳其代码?

回答by ErickBest

May be late.... but,

可能会迟到......但是,

I'd prefer to go this way..

我宁愿走这条路..

$.extend($.expr[":"], {
"MyCaseInsensitiveContains": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});

This way, you DO NOTtamper with jQuery's NATIVE '.contains'... You may need the default one later...if tampered with, you might find yourself back to stackOverFlow...

这样,您就不会篡改 jQuery 的 NATIVE '.contains'...您以后可能需要默认的...如果被篡改,您可能会发现自己又回到了stackOverFlow...

回答by bresleveloper

i'll allow myself to add my friends:

我会允许自己添加我的朋友:

$.expr[":"].containsNoCase = function (el, i, m) { 
    var search = m[3]; 
    if (!search) return false; 
    return eval("/" + search + "/i").test($(el).text()); 
}; 

回答by Umesh Patil

I was just able to ignore jQuery's case sensitivity altogether to achieve what I want using below code:

我只是能够完全忽略 jQuery 的区分大小写,以使用以下代码实现我想要的:

            $.expr[":"].contains = $.expr.createPseudo(function(arg) {
            return function( elem ) {
                return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
            };
        });

You can use this link to find code based on your jQuery versions to ignore case sensitivity, https://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/

您可以使用此链接根据您的 jQuery 版本查找代码以忽略区分大小写, https://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/

Also if you want to use :contains and make some search you may want to take a look at this: http://technarco.com/jquery/using-jquery-search-html-text-and-show-or-hide-accordingly

此外,如果您想使用 :contains 并进行一些搜索,您可能想看看这个:http: //technarco.com/jquery/using-jquery-search-html-text-and-show-or-hide-因此