使用单个括号时jQuery选择器问题 [英] jQuery selector problem when using a single parenthesis
问题描述
如果我在一个包含选择器中包含一个括号,我会收到一个错误。
If I include a single parenthesis in a :contains selector I get an error.
例如
$("a:contains('Speed (mph')")
返回错误
语法错误,无法识别的表达式:
(mph')
Syntax error, unrecognized expression: (mph')
如果我添加在最后的括号中,该语句运行正常,但是,我需要能够查询哪里常常有一个括号。
if I add the closing parenthesis at the end, the statement runs fine. however, I need to be able to query where there is often a single parenthesis.
这里发生了什么,如何我回避这个?
What's going on here and how do I sidestep this?
编辑
在我的实际代码中:包含部分以变量
In my actual code the :contains part is passed in as a variable
例如
var searchText = 'Speed (mph';
var result = $("a:contains('" + searchText + "')");
推荐答案
你应该可以使用 $(a:contains('Speed \\\((mph'))
从 http://api.jquery。 COM / category / selectors /
如果您想使用任何
元字符(如
!#$%&'()* +,。/:;< =>?@ [] ^`{|}〜)作为一个
字面部分的一个名字,你必须
使用两个
反斜杠来转义字符:\。例如,如果
具有id =foo.bar的元素,则
可以使用选择器$(#foo\.bar)。
W3C CSS规范包含
有关
CSS选择器的完整规则。
If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, you must escape the character with two backslashes: \. For example, if you have an element with id="foo.bar", you can use the selector $("#foo\.bar"). The W3C CSS specification contains the complete set of rules regarding valid CSS selectors.
这篇关于使用单个括号时jQuery选择器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!