admin管理员组文章数量:1406937
Most DOM query methods are available on both Document
s and Element
s. 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 Document
s and Element
s. 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?
- 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
3 Answers
Reset to default 13 +50The 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 +0000Remove 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
版权声明:本文标题:javascript - Why isn't getElementById() available on Elements? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745053137a2639774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论