JQuery 1.7.1貌似无法处理HTML5元素ID [英] JQuery 1.7.1 seemingly can't handle HTML5 element IDs
问题描述
您可能知道, HTML5允许在ID名称中使用更多字符 - 请参阅 HTML5规范现在有空格作为唯一的无效字符。试图在JQuery中使用它会显示JQuery忽略特定有效字符后的ID中的所有字符,'/'。
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>
记录'foo / bar'元素
Logging the 'foo/bar' element
console.log($(document).find('div#foo/bar'))
显示返回的元素不正确:
Shows the incorrect element being returned:
[
<div id="foo">
BOO
</div>
]
这是使用当前稳定的JQuery(1.7.1)和当前的JQuery边缘。
This is using both the current stable JQuery (1.7.1) and the current JQuery edge.
是这是一个JQuery错误,还是我做错了什么?
Is this a JQuery bug, or am I doing something wrong?
推荐答案
逃避斜线(演示: http://jsfiddle.net/m9NT8/ ):
console.log($(document).find('div#foo\\/bar'))
PS。 $(document).find('selector')
相当于 $(选择器)
。
此行为在RegEx中定义 Sizzle的源代码,第374行:
This behaviour is defined in a RegEx at Sizzle's source code, line 374:
ID: /#((?:[\w\u00c0-\uFFFF\-]|\\.)+)/,
这篇关于JQuery 1.7.1貌似无法处理HTML5元素ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!