我不知道为什么,但这段代码不起作用?为什么不呢?我想这是因为我在这里遇到范围问题:
function washAway(obj) {
alert($(obj)); // says HTML Object which is fine
setTimeout(function() {
alert($(obj)); // says undefined
$(obj).fadeOut("slow", function() {
$(this).remove();
});
}, 2000);
};
最佳答案
在超时中的函数执行时,它无法知道 obj 是什么 - 它是传递到设置超时的方法中的参数,但里面的函数没有提及它。
另一种方法是编写一个 jQuery 插件来等待它的行为,如下所示:
function($){ //to protect $ from noConflict()
$.fn.pause = function(duration) {
$(this).animate({ dummy: 1 }, duration);
return this;
};
}
然后你可以使用:
$(obj).pause(2000).fadeOut("slow", function() { $(this).remove(); });
关于asp.net - SetTimeOut 的范围问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2032918/