在 Javascript 中,你能扩展 DOM 吗? [英] In Javascript, can you extend the DOM?

查看:29
本文介绍了在 Javascript 中,你能扩展 DOM 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Javascript 中,您可以使用其原型对象来扩展现有类:

In Javascript, you can extend existing classes by using its prototype object:

String.prototype.getFirstLetter = function() {
    return this[0];
};

是否可以使用这种方法来扩展DOM元素?

Is it possible to use this method to extend DOM elements?

推荐答案

您可以使用 Element 的原型来扩展 DOM.但是,这在 IE7 及更早版本中不起作用.您将需要扩展特定元素,一次一个.Prototype 库就是这样做的.我建议查看 source 以确切了解它的情况完成.

you can extend the DOM by using the Element's prototype. However, this does not work in IE7 and earlier. You will need to extend the specific element, one at a time. The Prototype library does this. I recommend looking through the source to see exactly how it's done.

这篇关于在 Javascript 中,你能扩展 DOM 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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