我有一个同事问我为什么他不能从回调函数中访问事件参数。事实证明,jquery 似乎在调用完成后将事件设置为 null 并创建一个临时局部变量解决了问题(见下文)。
然后这让我想到,为什么“消息”甚至可用于回调。有人可以解释一下吗?
$('some seletor').click({msg: message},function(event){
alert(event.data.msg); //event.data.msg is not available to the json callback because event is null
var message = event.data.msg; //message will be available to the callback...why?
$.getJSON('ajax/test.json', function(data) {
alert(message); //works ok - why is the message variable visible here at all, should it not have gone out of scope when the above function ended?
alert(event.data.msg); //will crash, seems that event has been set to null by the framework after the function finished
});
});