开发者

What JS objects can be added through appendChild()?

开发者 https://www.devze.com 2023-02-03 06:37 出处:网络
I am trying to extend the DOM. I \"subclassed\" f开发者_JAVA百科rom the Div element: var Subclass = function() {}

I am trying to extend the DOM. I "subclassed" f开发者_JAVA百科rom 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?


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.


According to W3C specification appendChild():

  1. Returns Node.
  2. Throws DOMException.
  3. Requires one argument of type Node.

From specification:

Node appendChild(in Node newChild) raises(DOMException);


if you want to extend only div elements then may be you can do that by

HTMLDivElement.prototype.newFunction = function () { //'this' will get that div dom }

then it will extend only the div elements.. works on FF, don't know about other browsers.

hope this may help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消