javascript中var that=this

在JavaScript中,this代表的是当前对象。

$(‘#conten').click(function(){
//this是被点击的#conten
var that = this;
$(‘.conten').each(function(){
//this是.conten循环中当前的对象
//that仍然是刚才被点击的#conten
});
});

  可以看到,this对象在程序中随时会改变,而var that=this之后,that没改变之前仍然是指向当时的this,这样就不会出现找不到原来的对象。

posted @ 2017-09-26 11:11  前端HL  阅读(1259)  评论(0)    收藏  举报