简体   繁体   English

在 Javascript 中,你能扩展 DOM 吗?

[英]In Javascript, can you extend the DOM?

In Javascript, you can extend existing classes by using its prototype object:在 Javascript 中,您可以使用其原型对象来扩展现有类:

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

Is it possible to use this method to extend DOM elements?是否可以使用这种方法来扩展 DOM 元素?

I found the answer just as I was writing the question, but thought I'd post anyway to share the info.我在写问题的时候找到了答案,但我想我还是会发帖来分享信息。

The object you need to extend is Element.prototype .您需要扩展的对象是Element.prototype

Element.prototype.getMyId = function() {
    return this.id;
};

you can extend the DOM by using the Element's prototype.您可以使用 Element 的原型来扩展 DOM。 However, this does not work in IE7 and earlier.但是,这在 IE7 及更早版本中不起作用。 You will need to extend the specific element, one at a time.您将需要扩展特定元素,一次一个。 The Prototype library does this. Prototype 库就是这样做的。 I recommend looking through the source to see exactly how it's done.我建议查看源代码以确切了解它是如何完成的。

You shouldn't be directly extending anything (by "anything" I mean native DOM objects) - that will only lead to bad things.你不应该直接扩展任何东西(“任何东西”我的意思是原生 DOM 对象)——那只会导致不好的事情。 Plus re-extending every new element (something you'd have to do to support IE) adds additional overhead.加上重新扩展每个新元素(你必须做的事情来支持 IE)会增加额外的开销。

Why not take the jQuery approach and create a wrapper/constructor and extend that instead:为什么不采用 jQuery 方法并创建一个包装器/构造函数并对其进行扩展:

var myDOM = (function(){
    var myDOM = function(elems){
            return new MyDOMConstruct(elems);
        },
        MyDOMConstruct = function(elems) {
            this.collection = elems[1] ? Array.prototype.slice.call(elems) : [elems];
            return this;
        };
    myDOM.fn = MyDOMConstruct.prototype = {
        forEach : function(fn) {
            var elems = this.collection;
            for (var i = 0, l = elems.length; i < l; i++) {
                fn( elems[i], i );
            }
            return this;
        },
        addStyles : function(styles) {
            var elems = this.collection;
            for (var i = 0, l = elems.length; i < l; i++) {
                for (var prop in styles) {
                    elems[i].style[prop] = styles[prop];
                }
            }
            return this;
        }
    };
    return myDOM;
})();

Then you can add your own methods via myDOM.fn ... And you can use it like this:然后你可以通过myDOM.fn添加你自己的方法......你可以像这样使用它:

myDOM(document.getElementsByTagName('*')).forEach(function(elem){
    myDOM(elem).addStyles({
        color: 'red',
        backgroundColor : 'blue'
    });
});

Yes you can, but it is strongly advised not to.可以,但强烈建议不要这样做。

If you override something another library is expecting to be the original or another library overwrote something you were expecting .. chaos!如果您覆盖了另一个库期望成为原始库的内容,或者另一个库覆盖了您期望的内容.. 混乱!

It is best practice to keep your code in your own namespace/scope.最佳做法是将您的代码保存在您自己的命名空间/范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM