2011-06-17 75 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