通过appendChild()可以添加哪些JS对象?
我正在尝试扩展 DOM。我从 Div 元素“子类化”:
var Subclass = function() {}
Subclass.prototype = document.createElement('div');
Subclass.constructor = Subclass;
var obj = new Subclass();
var obj.innerHTML = 'test';
document.body.appendChild(obj); // Exception: Node cannot be inserted ... point in the hierarchy
那么,如果对象的原型是 DOM 对象不行,那么将对象插入到 DOM 中的要求是什么?
I am trying to extend the DOM. I "subclassed" from the Div element:
var Subclass = function() {}
Subclass.prototype = document.createElement('div');
Subclass.constructor = Subclass;
var obj = new Subclass();
var obj.innerHTML = 'test';
document.body.appendChild(obj); // Exception: Node cannot be inserted ... point in the hierarchy
So if an object's prototype being a DOM object won't do, what is the requirement for an object to be inserted into the DOM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这实际上在 Firefox 中是有效的……无论如何。在执行 new Subclass 时,它实际上不会创建新元素,而是继续使用相同的元素。
无论如何,我想说 DOM 对象本身是“特殊的”,因为只有它们才能被追加子代。
This actually works in Firefox.. Sort of, anyway. It won't actually create new elements when doing new Subclass, but keeps using the same.
In any case, I would say the DOM objects themselves are "special" in the way that only they can be appendChild'd.
根据 W3C 规范
appendChild()
:Node
。DOMException
。Node
类型的参数。来自规范:
According to W3C specification
appendChild()
:Node
.DOMException
.Node
.From specification:
如果您只想扩展 div 元素,那么您可以这样做,届时
它将仅扩展 div 元素..适用于 FF,不知道其他浏览器。
希望这可以帮助你。
if you want to extend only div elements then may be you can do that by
then it will extend only the div elements.. works on FF, don't know about other browsers.
hope this may help you.