2012-09-21 44 views
0

我正在寫一個jQuery插件,我想該插件安裝到選擇的元素,它是目前唯一時,有任何點擊或其他事件沒有jQuery插件模板chainability與每準備

這個插件有它其他的鏈接函數,我想在插件附加到這個元素時調用這個函數。

而且我想將該元素作爲對象傳遞給鏈接函數。

但我得到這個錯誤信息,當然插件不工作!我得到同樣的錯誤是否我該插件使用eachready

return this.each(function() {...} 

return this.ready(function() {...) 

TypeError: $this.function_1 is not a function

如何作出正確的?

jQuery的,

$(document).ready(function(){ 

     $('.box').selection(); 

    }); 


    (function($){ 

     $.fn.extend({ 

      selection: function(options) { 

       var defaults = { 
        element: "selection" 
       } 

       var options = $.extend(defaults, options); 
       var o = options; 

       // Must declare a this variable outside the core if you have other children functions inside this plugin. 
       var $this = this; 

       return this.each(function() { 

        // Set the object. 
        var object = $(this); 
        //alert(object.html()); 

        // Attach the functions. 
        $this.function_1(object); 
        $this.function_2(object); 


       }); 

       // function_1 
       $this.function_1 = function(object) 
       { 
        alert('function 1'); 

       } 

       // function_2 
       $this.function_2 = function(object) 
       { 
        alert('function 2'); 
       } 

      } 
     }); 

    })(jQuery); 

HTML,

<div class="box"> 

    <ul class="selection"> 
     <li>1</li> 
     <li>2</li> 
     <li>3</li> 
    </ul> 

</div> 

jsfiddle

回答

0

您在聲明函數之前調用return。將呼叫上方的函數聲明移至return,它可以工作。

+0

aww ...笨我!非常感謝! – laukok