简体   繁体   English

W3C DOM4规范的5.2.2节中的jQuery注释

[英]jQuery note in Section 5.2.2 of W3C DOM4 Specification

Is there anyone that can explain the note on section 5.2.2 of the W3C DOM4 specification ? 是否有人可以解释W3C DOM4规范第5.2.2节的注释?

Relevant Quote: 相关报价:

Note: The getElementById() method is not on elements for compatibility with older versions of jQuery. 注意: getElementById()方法不在元素上,以便与旧版本的jQuery兼容。 If a time comes where that version of jQuery has disappeared, we might be able to support it. 如果时间到了那个版本的jQuery消失了,我们也许能够支持它。

I'm curious how this interface would explicitly cause a problem with jQuery and what versions, does anyone have an example? 我很好奇这个接口如何明确地导致jQuery的问题以及哪些版本,有没有人有一个例子?

To expand on @Nan answer, it probably has something to do with jQuery using getElementById to validate a step in an iteration. 为了扩展@Nan答案,它可能与jQuery有关,使用getElementById来验证迭代中的步骤。 Adding this method to HTMLElement would make some conditions validate when part of jQuery code depends on it not validating. 将此方法添加到HTMLElement会使一些条件验证jQuery代码的一部分依赖于它不验证。

Hard to say exactly which version causes the problem and in exactly which situations, but a quick look to old jQuery versions, you can see that find() in older version isn't compatible with Elements having getElementById method. 很难确切地说出哪个版本导致了问题,并且确切地说在哪些情况下,但是快速查看旧的jQuery版本,您可以看到旧版本中的find()与具有getElementById方法的元素不兼容。

Going back to version 1.3, you can try to add the method to HTMLElement and you'll see that it messes the result. 回到1.3版,你可以尝试将方法添加到HTMLElement,你会发现它混淆了结果。 More recent version handle this correctly. 更新版本正确处理此问题。 See snippet: 请参阅代码段:

 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> 

It looks like the getElementById method is only present on document global object, and it's not part of the DOM4 Element object yet . 它看起来像getElementById方法只出现在document全局对象,这是不是DOM4的一部分Element的对象

This is due to compatibility issues with an older version of jQuery as you can read on DOM4 specification. 这是由于与旧版jQuery的兼容性问题,因为您可以阅读DOM4规范。

But, what does all this means? 但是,这一切意味着什么呢? it means that W3C tried to add this method into the Element object and also means that once this "version of jQuery" disapear we "might" be able to chain getElementById() calls like this: 这意味着W3C试图将此方法添加到Element对象中,并且还意味着一旦这个“jQuery版本”消失,我们“可能”能够像这样链接getElementById()调用:

var myElement = document.getElementById("header").getElementById("slogan");

Nothing special, they didn't want the most popular DOM manipulation wrapper to crash or jQuery as a member of the W3C has had some influence over this decision 没有什么特别的,他们不希望最流行的DOM操作包装器崩溃或jQuery作为W3C成员对此决定有一些影响

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM