gpt4 book ai didi

jquery选择器包含等号

转载 作者:行者123 更新时间:2023-12-03 22:47:11 24 4
gpt4 key购买 nike

我正在使用 jQuery 选择器来检查 href 中的字符:

var param = "something";
$("a[href*="+param+"]").css("color", "red");

这工作正常,直到“something”包含 = 符号。例如:

var param = "xkey=123";
$("a[href*="+param+"]").css("color", "red");

然后我没有得到任何结果。有人知道解决办法吗?

最佳答案

您需要转义 = 字符,因为它在 jQuery 中使用时具有特殊含义 selector 。以下是需要转义的所有字符的完整列表:

#;&,.+*~':"!^$[]()=>|/

因此,您可以使用以下内容来转义param:

param.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1')

来自 jQuery documentation :

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

这是一个 DEMO

关于jquery选择器包含等号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208390/

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