为什么斜杠字符会导致我的选择器失败?

Why is the slash character causing my selector to fail?

在 IE 11 上使用 JQuery1.6.4

我有一个 ID 中带有斜线的元素。

<span id='a/b'>
test
</span>

在我的代码中,我正在做

alert($('#a/b').length);

输出为 0。仅当 ID 中有斜线 (/) 时才会出现这种情况。 document.getElementById('a/b') 运行正常。

所以我很困惑为什么带有斜杠的 id 在 JQuery 中不起作用?

不是IE的问题

那是因为/是元字符,不能直接使用。使用时必须转义。

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

To use any of the meta-characters (such as !"#$%&'()*+,./:;<=>?@[\]^``{|}~) as a literal part of a name, it must be escaped with with two backslashes: \.

For example, an element with id="foo.bar", can use the selector $("#foo\.bar").