【发布时间】:2011-03-20 21:57:10
【问题描述】:
根据 jQuery 文档,当我的选择器字符串中出现的元字符作为文字出现时,我需要对它们进行转义。但是,我找不到很多关于何时以及何时不转义选择器的具体示例。所以什么时候什么时候不需要转义元字符,当它们被解释为文字时,在:
属性选择器?即
$("[attr=value]")
ID 选择器?即
$("#id")
类选择器?即
$(".class");
还有,有没有办法编写一个函数来替换选择器字符串中的元字符,同时仍然保留起始字符?即:
// replace all meta chars, preserving the id selection?
$("#id.rest$of*string")
// replace all meta chars, preserving the attribute selection?
// going back to my previous question, do I even need to escape the metachars in this situation?
$("[attr=blah.dee#foo*yay]")
我问这个问题的原因是因为我正在使用一个碰巧有一些非常讨厌的选择器的网站。而且我无法控制网站,所以我无法更改选择器以更好地使用。
谢谢!!
【问题讨论】:
标签: javascript jquery jquery-selectors escaping