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

发布于 2024-08-19 20:41:51 字数 950 浏览 6 评论 0原文

我试图不区分大小写地使用“包含”。我尝试在以下 stackoverflow 问题中使用该解决方案,但它不起作用:

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

为了方便起见,将解决方案复制到此处:

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

这是错误:

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

>这是我使用它的地方:

  $('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");
    }
  });

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

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

Is there a case insensitive jQuery :contains selector?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦开始←不甜 2024-08-26 20:41:52

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

我更愿意走这条路......

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

这样,你不要篡改 jQuery 的原生'.contains'......您稍后可能需要默认值...如果被篡改,您可能会发现自己回到了 stackOverFlow...

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 NOT tamper with jQuery's NATIVE '.contains'... You may need the default one later...if tampered with, you might find yourself back to stackOverFlow...

夜无邪 2024-08-26 20:41:52

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

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

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()); 
}; 
自在安然 2024-08-26 20:41:52

我只是能够完全忽略 jQuery 的大小写敏感度,以使用下面的代码实现我想要的效果:

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

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

另外,如果您想使用 :contains 并进行一些搜索,您可能需要看看这个:http://technarco.com/jquery/using-jquery-search-html-text-and -相应地显示或隐藏

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

            $.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/

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

若言繁花未落 2024-08-26 20:41:51

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

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

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

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

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.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.

晚风撩人 2024-08-26 20:41:51

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

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

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

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

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

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.

简单 2024-08-26 20:41:51

我会做这样的事情

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

并留下 :contains 单独...

DEMO

那么为什么 jQuery 不这样做它的库不支持它?!如果这么简单...

因为你的代码通过了火鸡代码吗?

I would do something like this

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

And Leave :contains Alone...

DEMO

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

because Does your code pass the turkey code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文