开发者

IE doesn't respect the sequence of javascript line execution?

开发者 https://www.devze.com 2023-04-12 19:28 出处:网络
I\'m trying to animate using jQuery but on IE7/8/9 it\'s not working before I show the element. function callback() {

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 th开发者_JAVA百科e element that has been shown, before execute the callback. What is the problem here?


You need to wait for the element to exist within the page before you can select it with jQuery.

wrap your script with:

jQuery(function($){
  //your code here
});

It's a shortcut for the document.ready event listener.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消