How to inherit from the DOM element class

后端 未结 5 478
[愿得一人]
[愿得一人] 2020-11-30 07:59

I want to write some Javascript classes which extend DOM nodes (so that I can then insert instances of my class directly into the DOM), but am having difficulty finding out

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 09:01

    I've found a hack that works... at least, it enables me to access the extended object properties via the DOM element and vice versa. But it's hardly elegant.

    var DOMelement = document.getElementById('myID'); // or  $('#myID')[0]; in jQuery
    DOMelement.extended = new extensionClass(this);
    
    function extensionClass(element) {
        this.element = element;
        ...
    }
    

提交回复
热议问题