What JS objects can be added through appendChild()?
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()
:
- Returns
Node
. - Throws
DOMException
. - 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.
精彩评论