<button>点击我</button>
$('button').click(function () {
setTimeout(function () {
console.log(this);
}, 2000);
})
一、改变定时器中的this指向,this指向button
$('button').click(function () {
var that=this;//在定时器外部声明一个that保存this
setTimeout(function () {
console.log(that);
}, 2000);
})
二、改变定时器中的this指向,this指向button
$('button').click(function () {
//bind()---把this绑定到定时器上,等待一秒再执行。
setTimeout(function () {
console.log(this);
}.bind(this), 1000);
//}.bind($('button')), 1000);
})