【发布时间】:2018-04-22 17:26:26
【问题描述】:
我是 Jquery 的新手,正在阅读“JQueryInAction”一书。 我从这本书中看到了这个例子:
$(function(){
$('*').each(function(){
var current = this;
this. onclick = function(event) {
if (!event) event = window.event;
var target = (event.target) ? event.target : event.srcElement;
say('For ' + current.tagName + '#'+ current.id +
' target is ' + target.id);
}
});
});
这里我真的不明白在第 3 行使用局部变量 current 而不是 this。
注意:我了解 JavaScript 并且了解 closures 以及 this 如何在 closure 中不可用但这里不是这样,this 在事件处理程序中可用。
这里current有什么意义。
【问题讨论】:
标签: javascript jquery function closures