问题
As you may be aware, HTML5 allows more characters to be used in ID names - see the HTML5 spec which now has space as the only invalid character. Attempting to use this with JQuery shows JQuery ignoring all characters in the ID after a particular valid character, '/'.
<section>
<div id='foo/bar'>
YAAY
</div>
<div id='foo'>
BOO
</div>
</section>
Logging the 'foo/bar' element
console.log($(document).find('div#foo/bar'))
Shows the incorrect element being returned:
[
<div id="foo">
BOO
</div>
]
This is using both the current stable JQuery (1.7.1) and the current JQuery edge.
Is this a JQuery bug, or am I doing something wrong?
回答1:
Escape the slash (demo: http://jsfiddle.net/m9NT8/):
console.log($(document).find('div#foo\\/bar'))
PS. $(document).find('selector')
is equivalent to $(selector)
.
This behaviour is defined in a RegEx at Sizzle's source code, line 374:
ID: /#((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,
来源:https://stackoverflow.com/questions/9750670/jquery-1-7-1-seemingly-cant-handle-html5-element-ids