简体   繁体   English

jquery使用包含选择器查找异常

[英]jquery find exception with contains selector

I have some code running with jQuery 1.7.2 I encountered an error when trying to find using this selector 我有一些运行jQuery 1.7.2的代码我在尝试使用此选择器时遇到错误

var x = "span:contains(\"C) Foo (Bar)\")";
$('body').find(x)

https://jsfiddle.net/elewinso/dfbn82wo/7/ https://jsfiddle.net/elewinso/dfbn82wo/7/

This issue is fixed in JQuery 1.8 and up but since I cannot switch my jQuery I was hoping to find a patch. 这个问题在JQuery 1.8及更高版本中修复,但由于我无法切换我的jQuery,我希望找到一个补丁。

The problem is your selector is confusing jQuery's parser because it contains brackets. 问题是你的选择器混淆了jQuery的解析器,因为它包含括号。 It thinks the ) you are trying to match is the closing bracket of its contains: content filter. 它认为)你试图匹配是其右括号contains:内容过滤器。

You can get around this by using filter() instead. 你可以通过使用filter()来解决这个问题。

$('span').filter(function() {
    return $(this).text().indexOf('"C) Foo (Bar)"') != -1;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM