How to add new methods to the jQuery object?

后端 未结 2 617
夕颜
夕颜 2020-12-29 07:17

Is there some way to add methods to jQuery\'s objects?

For example, I have jQuery object

a = $(\'div\')

I\'d like that every object

2条回答
  •  被撕碎了的回忆
    2020-12-29 07:51

    add it like

    $.fn.doSomething = function(data){
        // data is the argument passed to doSomething
        return this.each(function(){
           var elem = $(this);
           //perform operation on elem
           // like to change background color use
           elem.css('background-color','red');
    
        });
    }
    

提交回复
热议问题