IE 不尊重 javascript 行执行的顺序?
我正在尝试使用 jQuery 制作动画,但在 IE7/8/9 上,在显示元素之前它不起作用。
function callback() {
$('#content').animate([...]);
[...]
}
$('#content').hide();
[...]
$('#content').show();
callback();
它只对我有效,当 do setTimeout(function() {callback(); }, 300);
时,也许我需要等待 IE 识别已显示的元素,然后再执行打回来。这里有什么问题呢?
I'm trying to animate using jQuery but on IE7/8/9 it's not working before I show the element.
function callback() {
$('#content').animate([...]);
[...]
}
$('#content').hide();
[...]
$('#content').show();
callback();
It only works to me when a do setTimeout(function() { callback(); }, 300);
, maybe I need to wait the IE to recognize the element that has been shown, before execute the callback. What is the problem here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要等待该元素存在于页面中,然后才能使用 jQuery 选择它。
用以下内容包装您的脚本:
它是
document.ready
事件侦听器的快捷方式。You need to wait for the element to exist within the page before you can select it with jQuery.
wrap your script with:
It's a shortcut for the
document.ready
event listener.由于 JS 是单线程的,仅仅因为你调用 show() 并不意味着它实际上正在显示;您需要将控制权返回给父级,以允许它在此之前绘制和更新 DOM。 setTimeout 允许您的 JS 屈服于父级,在继续执行之前进行更新。 setTimeout 设置为 0 很可能也会起作用。
Since JS is single-threaded just because you call show() doesn't mean it's actually showing; you need to return control to the parent to allow it to draw and update the DOM before that happens. setTimeout allows your JS to yield to the parent, making it's updates before you continue with execution. setTimeout with 0 will most likely also work.