为什么文档具有getElementById方法,即使它不是从Element继承的



在MDN文档之后,Element接口保存了搜索像这样的dom节点的所有方法

  • getElementById
  • getElementsByTagName

MDN还声明Document直接从Node继承NOT来自Element。

那么,为什么Document持有所有这些Element方法(以及ParentNode接口中的方法(呢?MDN只是没有跟上规格,还是我错过了什么?

Document.prototypeElement.prototype都有getElementsByTagName。其中一个不是从另一个继承的——它们是完全独立的函数(无意中(:

console.log(
Element.prototype.hasOwnProperty('getElementsByTagName'),
Document.prototype.hasOwnProperty('getElementsByTagName'),
Element.prototype.getElementsByTagName === Document.prototype.getElementsByTagName,
Document.prototype.hasOwnProperty('getElementById'),
Element.prototype.hasOwnProperty('getElementById'),
);

Element.prototype具有getElementById

ParentNode接口是抽象规范,而不是您可以在某个地方检查的实际Javascript对象。Element.prototypeDocument.prototype都实现了它,但它们是通过将ParentNode方法直接放在原型上来实现的。(ParentNode与Node完全不同(

最新更新