What JS objects can be added via appendChild ()?

I am trying to extend the DOM. I am "subclassed" from a 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 the prototype of the object being the DOM object fails, what is the requirement to insert the object into the DOM?

+3
source share
3 answers

It really works in Firefox .. Anyway. In fact, it will not create new elements when creating a new subclass, but will continue to use the same.

In any case, I would say that the DOM objects themselves are "special" in the sense that only they can be appendChild'd.

0
source

According to the W3C specification appendChild():

  • Returns Node.
  • DOMException.
  • Node.

:

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

if you want to expand only div elements, maybe you can do it with

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

then it will expand only div elements. Works on FF, doesn't know about other browsers.

Hope this can help you.

0
source

Source: https://habr.com/ru/post/1783788/


All Articles