如何将自己的方法添加到HTMLElement对象? [英] How to add my own methods to HTMLElement object?

查看:163
本文介绍了如何将自己的方法添加到HTMLElement对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如对于 this.parentNode 我想写 this.p 或代替
document.getElementById('someid')只需写 document.g('someid')。当然这只是一个简单的例子,我只是想知道这样做的正确方法。

For example for this.parentNode I would like to just write this.p or instead of document.getElementById('someid') just write document.g('someid'). Of course that are simple examples, I just want to know what is the correct way to do it.

(我知道我可以使用jQuery或Prototype,但我会想知道它是如何在JS中完成的那样)

(I know I can use jQuery or Prototype, but I'd like to learn how it is really done in JS)

推荐答案

虽然你可以原型<许多浏览器中的code> HTMLElement - Internet Explorer(6,7,8) NOT 其中之一。 AFAIK,IE9支持这一点(虽然我还没有测试过)。

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);
};

这篇关于如何将自己的方法添加到HTMLElement对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆