gpt4 book ai didi

使用单括号时的 jQuery 选择器问题

转载 作者:行者123 更新时间:2023-12-01 04:26:01 25 4
gpt4 key购买 nike

如果我在 :contains 选择器中包含单个括号,则会收到错误。

例如

$("a:contains('Speed (mph')")

返回错误

Syntax error, unrecognized expression: (mph')

如果我在末尾添加右括号,则该语句运行正常。但是,我需要能够查询哪里经常有一个括号。

这是怎么回事?我该如何避免这种情况?

编辑

在我的实际代码中,:contains 部分作为变量传入

例如

var searchText = 'Speed (mph';
var result = $("a:contains('" + searchText + "')");

最佳答案

您应该能够使用$("a:contains('Speed\\(mph')")

来自http://api.jquery.com/category/selectors/

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 选择器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6469993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com