为什么jQuery要return this.each??

jQuery.fn.test1= function(){ 
   this.css("background","#ff0");//这里面的this为jquery对象,而不是dom对象 
   return this.each(function(){ //遍历匹配的元素,此处的this表示为jquery对象,而不是dom对象 
          
          }); 

};

this.css(),this.each()里面的this为jquery对象

为什么要return this.each()

先return this.each(),后调用each()方法,而each()方法返回jQuery对象,所以这样就可以继续链式操作了。

比如我们调用方法 $('#test').test1() 后返回的是jQuery对象,这样可以继续在后面用$('#test').test1().color('red');