JQuery remark in section 5.2.2 of the W3C DOM4 spec

Is there anyone who can explain the note on in section 5.2.2 of the W3C DOM4 spec ?

Relevant quote:

Note. The method is getElementById()

not a legacy jQuery compatibility element. If the time comes when this version of jQuery is gone, we might be able to support it.

I am curious how this interface will explicitly cause the problem with jQuery

and what versions, does anyone have an example?

+3


source to share


2 answers


To expand on @ Nan's answer, it probably has something to do with jQuery, using getElementById

to check a step on iteration. Adding this method to HTMLElement

will cause some conditions to be validated when a piece of jQuery code depends on it without checking.

It's hard to tell which version is causing the problem and in which situations, but a quick look at older versions of jQuery, you can see that find()

the older version is not compatible with elements that have a method getElementById

.

Going back to version 1.3, you can try adding a method to the HTMLElement and you will see that it messes up the result. A later version handles 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>
      

Run codeHide result


+2


source


It looks like the method getElementById

is only present on the global object document

and it is not part of the DOM4 object yet Element

.

This is due to compatibility issues with older version of jQuery, which you can read in the DOM4 spec.

But what does all this mean? this means the W3C tried to add this method to the object Element

, and also means that once this "jQuery version" disappears, we "can" bind the calls getElementById()

like this:



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

      

Nothing fancy, they didn't want the most popular wrapper DOM manipulation crash or jQuery as a W3C member to have some impact on this decision

+1


source







All Articles