我使用jQuery选择器来检查href中的字符:
@H_404_2@var param = "something";
$("a[href*="+param+"]").css("color","red");
这工作正常,直到“某事物”包含=符号.例如:
@H_404_2@var param = "xkey=123"; $("a[href*="+param+"]").css("color","red");然后我没有结果.有人知道解决方案吗?
解决方法
您需要转义=字符,因为它在jQuery
selector中使用时具有特殊含义.以下是需要转义的所有字符的完整列表:
@H_404_2@#;&,.+*~':"!^$[]()=>|/
因此,您可以使用以下命令来转义参数:
@H_404_2@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.