2011-06-17 52 views
0

我做的jQuery的這個簡單的擴展:基礎 - 訪問成員

(function($) 
    {  
     $.fn.extend({   

      animateleft: function(amount) {    
        $().animate({ 
          left: amount 
          }, 300, function() { }); 

        return $();  
       }  
     }); 
    })(jQuery); 

據我所知,通過返回$()它使鏈接。但是我不知道$()中包含的是什麼。

$('#container').animateleft("+=300"); 

這一點我想如果$(「#集裝箱」)是什麼傳遞到擴展爲$應該工作():當我嘗試像得到觸發似乎並不animate函數。

回答

2

在插件方法中,所選元素由this引用。因此,它應該是:

(function($) {  
    $.fn.extend({   
     animateleft: function(amount) {    
      this.animate({ 
       left: amount 
      }, 300, function() { }); 
      return this;  
     }  
    }); 
})(jQuery); 

$()只是調用不帶參數,它返回一個新的(空)jQuery對象jQuery的功能。

+0

非常好,謝謝。 – 2011-06-17 00:44:27