jquery - is not a function error

后端 未结 7 449
傲寒
傲寒 2020-11-30 02:06

Here is my code:

(function($){
    $.fn.pluginbutton = function (options) {
        myoptions = $.extend({ left: true });
        return this.each(function (         


        
相关标签:
7条回答
  • 2020-11-30 03:05

    change

    });
    
    
    $(document).ready(function () {
        $('.smallTabsHeader a').pluginbutton();
    });
    

    to

    })(jQuery); //<-- ADD THIS
    
    
    $(document).ready(function () {
        $('.smallTabsHeader a').pluginbutton();
    });
    

    This is needed because, you need to call the anonymous function that you created with

    (function($){
    

    and notice that it expects an argument that it will use internally as $, so you need to pass a reference to the jQuery object.

    Additionally, you will need to change all the this. to $(this)., except the first one, in which you do return this.each

    In the first one (where you do not need the $()) it is because in the plugin body, this holds a reference to the jQuery object matching your selector, but anywhere deeper than that, this refers to the specific DOM element, so you need to wrap it in $().

    Full code at http://jsfiddle.net/gaby/NXESk/

    0 讨论(0)
提交回复
热议问题