有没有人可以解释一下 section 5.2.2 of the W3C DOM4 specification 上的注释? ?
相关引用:
Note: The
getElementById()
method is not on elements for compatibility with older versions of jQuery. If a time comes where that version of jQuery has disappeared, we might be able to support it.
我很好奇这个接口(interface)如何明确地导致 jQuery
的问题以及什么版本,有人有例子吗?
最佳答案
为了扩展@Nan 的答案,它可能与 jQuery 使用 getElementById
来验证迭代中的步骤有关。将此方法添加到 HTMLElement
将使某些条件在部分 jQuery 代码依赖于它而不是验证时验证。
很难确切地说出是哪个版本导致了这个问题以及在什么情况下,但是快速查看旧的 jQuery 版本,您会发现旧版本中的 find()
与 Elements 不兼容具有 getElementById
方法。
回到 1.3 版,您可以尝试将方法添加到 HTMLElement 中,您会发现它弄乱了结果。较新的版本可以正确处理此问题。见片段:
alert('Without getElementById method on HTMLElement, length of $("div").find("#test") is ' + $('div').find('#test').length);
window.HTMLElement.prototype.getElementById = function(str){
console.log(str);
return str;
}
alert('With getElementById method on HTMLElement, length of $("div").find("#test") is ' + $('div').find('#test').length);
<script src="https://code.jquery.com/jquery-1.3.js"></script>
<div id="container"><div id="test"></div></div>
关于javascript - W3C DOM4 规范第 5.2.2 节中的 jQuery 说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31525935/