开发者

jQuery document.ready fired but elements aren't found

I have a problem that happens only on a specific computer(FFX 3.6.13,Windows 7,jQuery 1.4.3). Sometimes document.ready is fired but when trying to get elements to attach the event handlers,the ele开发者_如何学JAVAments don't exist!

the code goes something like this:

$(function(){
   window.initStart = true;
   $("#id_of_element").click(function()...);
   window.initEnd = $("#id_of_element");
});

the window.initStart/End are there for debugging,sometimes this code runs just fine,but sometimes window.initEnd is just a empty jQuery set(length == 0). What this means is that document.ready is always fired,but sometimes it is fired before elements are available.

Does anybody had this problem? what could the problem be?


One way that you could try to get around this would be with using .live instead of .click. The following code

$('#idOfDiv').live('click', function() { doStuff(); });

will attach the input function to the click event of everything that is dropped on the page with an id of 'idOfDiv' as soon as it makes it to the page. Whereas .click executes immediately, this should be attached no matter what time the divs make it to the page.

Cheers


There's an article on SitePoint that demonstrates how to sense when certain dom elements are available.

Also I know this is a version specific issue, but if you were on Jquery 1.5 the deferred objects stuff would be useful here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜