javascript `HTMLElement` 和 `Element` 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6581680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between `HTMLElement` and `Element`?
提问by Li Juan
What's the difference between HTMLElement
and Element
?
什么之间的区别HTMLElement
和Element
?
document.createElement("div") instanceof Element
gives true
document.createElement("div") instanceof Element
给出真
document.createElement("div") instanceof HTMLElement
gives true
document.createElement("div") instanceof HTMLElement
给出真
HTMLElement === Element
gives false
HTMLElement === Element
给出错误
回答by Felix Kling
An HTMLElement
is anElement
, as defined in the HTML extension to the DOM Level 2 Core specification:
AnHTMLElement
是Element
,如DOM Level 2 Core 规范的HTML 扩展中所定义:
interface HTMLElement : Element {
attribute DOMString id;
attribute DOMString title;
attribute DOMString lang;
attribute DOMString dir;
attribute DOMString className;
};
Element
(per specification) refers to the Element
interface as it is defined in the DOM Core Level 2 specification(there is also another DOM Corespecification(working draft) though).
Element
(每个规范)指的Element
是在DOM Core Level 2 规范中定义的接口(尽管还有另一个DOM Core规范(工作草案))。
Update:There are a lot of specifications out there and it is not totally clear which browsers use which one (to me).
更新:那里有很多规范,尚不完全清楚哪些浏览器使用哪个(对我而言)。
Maybe someone else has more insight...
也许其他人有更深入的了解...
But in any case, Element
is a more generic interface than HTMLElement
and the latter inherits from the former.
但无论如何,Element
是一个HTMLElement
比前者更通用的接口,后者继承了前者。
Update 2:A nice way to see the inheritance structure is to execute console.dir(elementReference)
for any element (works in Chrome/Safari, needs Firebug for Firefox).
更新 2:查看继承结构的一个好方法是console.dir(elementReference)
对任何元素执行(适用于 Chrome/Safari,需要 Firebug for Firefox)。
回答by jerluc
HTMLElement refers explicitly to an HTML element whereas Element may refer to an XML element. However, HTMLElement is technically a subset of Element.
HTMLElement 明确指代 HTML 元素,而 Element 可以指代 XML 元素。但是,HTMLElement 从技术上讲是 Element 的子集。
回答by Thomas
HTMLElements inherit from Element which inherit from Node.
HTMLElements 继承自 Element,而 Element 继承自 Node。
This means that your HTMLElement is simultaneously an 'instanceof' all three. The fact that it is an HTMLElement means it has an interface that's contains methods only an HTMLElement would need like attachEvent.
这意味着您的 HTMLElement 同时是所有三个的“实例”。它是一个 HTMLElement 的事实意味着它有一个接口,其中包含只有 HTMLElement 需要的方法,如 attachEvent。