JQuery 1.7.1 seemingly can't handle HTML5 element IDs

百般思念 提交于 2019-12-10 15:35:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!