我很长时间没有使用 Javascript,今天一直在刷新自己。一直让我感动的一件事是this
关键字。我知道在 jQuery 事件处理程序中,例如 click 事件,this
是指触发事件的元素。即使我的函数没有参数,如何this
传递给我作为回调的函数?
给定以下代码:
$("tr.SummaryTbRow").data("Animating", false);
$("tr.SummaryTbAltRow").data("Animating", false);
$("tr.SummaryTbRow").click(function () {
if ($(this).data("Animating") == false) {
if ($(this).next(".Graph").css("display") == "none") {
$(this).data("Animating", true);
//Part I am questioning.
setTimeout(function () {
$(this).data("Animating", false);
}(this), 550);
$(this).next(".Graph").slideRow('down', 500);
}
else {
$(this).data("Animating", true);
$(this).next(".Graph").slideRow('up', 500);
}
}
});
我试图弄清楚如何将带有类的元素表行传递SummaryTbRow
给我的 setTimeout 回调函数。jQuery 传递this
的方式与我使用匿名回调函数所做的事情相似吗?我的this
内部函数是指this
我传入的吗?
我知道我可以这样做:
setTimeout(function (element) {
$(element).data("Animating", false);
}(this), 550);
但我想弄清楚 jQuery 如何能够传递this
给我的回调函数,即使我的函数需要 0 个参数。