如何使 jQuery Contains 不区分大小写,包括 jQuery 1.8+?
我试图不区分大小写地使用“包含”。我尝试在以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能会迟到......但是,
我更愿意走这条路......
这样,你不要篡改 jQuery 的原生'.contains'......您稍后可能需要默认值...如果被篡改,您可能会发现自己回到了 stackOverFlow...
May be late.... but,
I'd prefer to go this way..
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...
我会允许自己添加我的朋友:
i'll allow myself to add my friends:
我只是能够完全忽略 jQuery 的大小写敏感度,以使用下面的代码实现我想要的效果:
您可以使用此链接根据您的 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:
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
这是我在当前项目中使用的,没有任何问题。看看你是否对这种格式有更好的运气:
在 jQuery 1.8 中,这种格式的 API 发生了变化,jQuery 1.8+ 版本的这种格式将是:
您可以在这里测试一下。有关 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:
In jQuery 1.8 the API for this changed, the jQuery 1.8+ version of this would be:
You can test it out here. For more detail on 1.8+ custom selectors, check out the Sizzle wiki here.
值得注意的是,答案是正确的,但仅涵盖
:Contains
,而不是别名:contains
,这可能会导致意外行为(或者可以通过设计用于高级应用程序)需要敏感和不敏感搜索)。这可以通过复制别名的扩展来解决:
我花了一段时间才弄清楚为什么它对我不起作用。
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:
Took me a while to work out why it wasn't working for me.
我会做这样的事情
并留下
:contains
单独...DEMO
那么为什么 jQuery 不这样做它的库不支持它?!如果这么简单...
因为你的代码通过了火鸡代码吗?
I would do something like this
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?