$(this)和this

$(this)属于jQuery,this属于js。

举个栗子先,

1 $("#desktop a img").each(function(index){
2 
3           alert($(this));
4 
5           alert(this);
6 
7 }

alert($(this));    弹出的结果是[object Object ]

alert(this);        弹出来的是[object HTMLImageElement]

$("#desktop a img")就是上文所指的$(this),从返回结果可以看出this是html中的对象—HTMLImageElement,而$(this)=jquery(),它是jQuery对象,说明$(this)将this从html对象转换为了jQuery对象,在使用this.attr()或者$(this).attr()时常常会出现“对象不支持此属性或方法”错误,有可能是两种调用方法搞混了,应该查询现在使用的方法属于html还是jQuery,要调用的属性是否支持。

jQuery中的this可以转换为js的:$(this)[0] = this。

猜你喜欢

转载自www.cnblogs.com/oliwang/p/9198592.html