admin管理员组

文章数量:1406937

Most DOM query methods are available on both Documents and Elements. For example,

console.assert(
  document.getElementsByTagName   && document.body.getElementsByTagName &&
  document.getElementsByClassName && document.body.getElementsByClassName &&
  document.querySelector          && document.body.querySelector &&
  document.querySelectorAll       && document.body.querySelectorAll
);

However, getElementById is only available on Document:

console.assert(document.getElementById);
console.assert(document.body.getElementById == undefined);

Why is this so?


The WHATWG DOM Living Standard tells us that:

Web patibility prevents the getElementById() method from being exposed on elements

The W3C DOM4 Remendation is a bit more specific:

The getElementById() method is not on elements for patibility with older versions of jQuery. If a time es where that version of jQuery has disappeared, we might be able to support it.

However, I still have a hard time understanding what the issue could be. How could the presence of this methods adversely affect the behaviour of jQuery or other libraries?

I've tried looking through old versions of jQuery (such as 1.0.0 and 1.7.0) to see if any of their use of getElementById hints at why this may have been a problem. I see that getElementById used to be buggy in some older browsers, but they detect that and fall back to a reliable manual implementation instead. I don't see anywhere that it could be called on an element and cause a bug. Where does this patibility concern e from?

Most DOM query methods are available on both Documents and Elements. For example,

console.assert(
  document.getElementsByTagName   && document.body.getElementsByTagName &&
  document.getElementsByClassName && document.body.getElementsByClassName &&
  document.querySelector          && document.body.querySelector &&
  document.querySelectorAll       && document.body.querySelectorAll
);

However, getElementById is only available on Document:

console.assert(document.getElementById);
console.assert(document.body.getElementById == undefined);

Why is this so?


The WHATWG DOM Living Standard tells us that:

Web patibility prevents the getElementById() method from being exposed on elements

The W3C DOM4 Remendation is a bit more specific:

The getElementById() method is not on elements for patibility with older versions of jQuery. If a time es where that version of jQuery has disappeared, we might be able to support it.

However, I still have a hard time understanding what the issue could be. How could the presence of this methods adversely affect the behaviour of jQuery or other libraries?

I've tried looking through old versions of jQuery (such as 1.0.0 and 1.7.0) to see if any of their use of getElementById hints at why this may have been a problem. I see that getElementById used to be buggy in some older browsers, but they detect that and fall back to a reliable manual implementation instead. I don't see anywhere that it could be called on an element and cause a bug. Where does this patibility concern e from?

Share edited Jan 20, 2019 at 22:09 Trump Voters Deserve Damnation asked Jan 18, 2019 at 16:44 Trump Voters Deserve DamnationTrump Voters Deserve Damnation 130k88 gold badges358 silver badges381 bronze badges 7
  • 4 Because id values must be unique throughout the entire document, what would be the point? – Pointy Commented Jan 18, 2019 at 16:46
  • 1 There may only be only one element, but I may only be interested in it if it exists in a particular subtree of the document. – Trump Voters Deserve Damnation Commented Jan 18, 2019 at 16:47
  • 1 @Pointy I have quoted both specifications' patibility concerns, so telling me it's because it's pointless seems incorrect. – Trump Voters Deserve Damnation Commented Jan 18, 2019 at 16:50
  • 2 I have no idea how or why an old version of jQuery would be a concern to the spec mittee. – Pointy Commented Jan 18, 2019 at 16:51
  • 2 @Pointy: Because introducing Element#getElementById breaks existing websites that use that version of jQuery, apparently widely deployed at least in 2013. – Ry- Commented Jan 18, 2019 at 16:57
 |  Show 2 more ments

3 Answers 3

Reset to default 13 +50

The git blame on https://github./w3c/dom’s master branch points to:

mit f71d7de304e1ee25573279157dd6ce1c2aa2c4f2
Author: Anne van Kesteren
AuthorDate: Tue Nov 26 13:53:41 2013 +0000
Commit: Anne van Kesteren <[email protected]>
CommitDate: Tue Nov 26 13:53:41 2013 +0000

Remove getElementById from Element. https://www.w3/Bugs/Public/show_bug.cgi?id=23860

and the linked bug describes how jQuery 1.2.5 + 1.2.6 (1.2.x?) are affected:

jQuery 1.2.5 assumes that any node it found in the DOM that has a "getElementById" property is a Document node. See https://bugzilla.mozilla/show_bug.cgi?id=933193#c17

As others have said in the ments, element IDs are supposed to be unique. There would be no need to have a getElementById method on an element.

According to MDN's article on getElementById:

Because ID values must be unique throughout the entire document, there is no need for "local" versions of the function.

The same is also true of getElementsByName

Having all your ids unique will really make your life easier.

本文标签: javascriptWhy isn39t getElementById() available on ElementsStack Overflow