Add jQuery function to specific elements

前端 未结 9 1502
悲&欢浪女
悲&欢浪女 2020-12-07 18:56

I know that you can add new jQuery functions by $.fn.someFunction = function()

However, I want to add functions to specific elements only. I tried this

9条回答
  •  清歌不尽
    2020-12-07 19:18

    yo can do the above with this:

    $.fn.testFn = function(){
        this.each(function(){
            var className = $(this).attr('class');
            $(this).html(className);
        });    
    };
    
    $('li').testFn(); //or any element you want
    

    Test: http://jsfiddle.net/DarkThrone/nUzJN/

提交回复
热议问题