gpt4 book ai didi

javascript - `HTMLElement` 和 `Element` 有什么区别?

转载 作者:IT王子 更新时间:2023-10-29 03:05:29 25 4
gpt4 key购买 nike

HTMLElementElement 有什么区别?

document.createElement("div") instanceof Element 给出 true

document.createElement("div") instanceof HTMLElement 给出 true

HTMLElement === Element 给出 false

最佳答案

一个HTMLElement 是一个 元素,定义在HTML extension to the DOM Level 2 Core specification :

interface HTMLElement : Element {
attribute DOMString id;
attribute DOMString title;
attribute DOMString lang;
attribute DOMString dir;
attribute DOMString className;
};

Element(根据规范)指的是在 DOM Core Level 2 specification 中定义的 Element 接口(interface)(不过还有另一个 DOM Core specification(工作草案))。

更新: 有很多规范,(对我而言)并不完全清楚哪些浏览器使用哪一个。

也许其他人有更多见识...

但无论如何,Element 是一个比HTMLElement 更通用的接口(interface),后者继承了前者。

更新 2: 查看继承结构的一个好方法是对任何元素执行 console.dir(elementReference)(在 Chrome/Safari 中工作,需要 Firebug火狐)。

关于javascript - `HTMLElement` 和 `Element` 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6581680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com