Axl Rose的wiki页面包含一个包含id的元素:
1983.E2.80.931986:_Early_years
我使用$(‘#1983.E2.80.931986:_Early_years’)获取此元素,但失败.
如果我想通过id选择这个元素,那么正确的选择器是什么?
解决方法
从
the docs直:
If you wish to use any of the Meta-characters (such as
!"#$%&'()*+,./:;<=>?@[\]^&a{|}~
) as a literal part of a name,you must
escape the character with two backslashes:\\.
For example,if you
have an element withid="foo.bar"
,you can use the selector$("#foo\\.bar")
.
推论:
jQuery.escapeSelector = function(str) { return str.replace(/[!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~]/g,"\\$&"); } // ... $('#' + $.escapeSelector('1983.E2.80.931986:_Early_years'))