Javascript 设置IE8中参数不工作时超时
有人能告诉我哪里出了问题吗?我已经在Firefox和Chrome中测试过了,它可以正常工作,只需要现在在IE8中工作就可以了Javascript 设置IE8中参数不工作时超时,javascript,internet-explorer-8,Javascript,Internet Explorer 8,有人能告诉我哪里出了问题吗?我已经在Firefox和Chrome中测试过了,它可以正常工作,只需要现在在IE8中工作就可以了 setTimeout(function(it) { x = $('.menuheader:first-child').position().left; w = $('.menuheader:first-child').width(); p = x + w + 16;
setTimeout(function(it) {
x = $('.menuheader:first-child').position().left;
w = $('.menuheader:first-child').width();
p = x + w + 16;
$(it).next().css('left', p);
$(it).next().show();
}, 200, this);
也试过
function showmenu(it){
x = $('.menuheader:first-child').position().left;
w = $('.menuheader:first-child').width();
p = x + w + 16;
$(it).next().css('left', p);
$(it).next().show();
}
window.setTimeout(function() {
showmenu(this)
}, 200);
我从未发现
setTimeout
参数特别可靠,所以我只做了以下几点:
var it = this;
setTimeout(function() { ... }, 200);
向无法接受参数的函数“传递参数”的正确传统方法是使用闭包:
var that = this;
setTimeout(function(){ doStuff(that);}, 200);
function doStuff(it) {
x = $('.menuheader:first-child').position().left;
w = $('.menuheader:first-child').width();
p = x + w + 16;
$(it).next().css('left', p);
$(it).next().show();
}
较新的替代方案(不兼容没有polyfill的IE8):
Duplicate of Duplicate对我也不起作用。是的,现在有了“that”就更好了。是的,你的第二次尝试没有起作用,因为JS通常会在调用函数时更改此。这就是为什么在通过闭包传递
此
时需要给它一个新名称的原因。
setTimeout(doStuff.bind(null, this), 200);