How to add my own methods to HTMLElement object?

后端 未结 4 1543
你的背包
你的背包 2020-11-27 22:25

For example for this.parentNode I would like to just write this.p or instead of document.getElementById(\'someid\') just write d

4条回答
  •  佛祖请我去吃肉
    2020-11-27 22:44

    Although you can prototype on the HTMLElement in many browsers - Internet Explorer (6,7,8) is NOT one of them. AFAIK, IE9 does support this (though I haven't tested it).

    For browsers that do handle it, you can do:

    HTMLElement.prototype.doHello = function(thing){
      alert('Hello World from ' + thing);
    };
    

提交回复
热议问题