admin管理员组文章数量:1389931
So I have this code:
var bindAll;
bindAll = function ()
{
$('#somediv').bind('mouseover', function(){do something});
};
var init;
init = function ()
{
bindAll();
...
};
$(document).ready(init());
and it does not work. But if I put the bind on a timer by replacing:
bindAll();
with
tt = setTimeout('bindAll()', 1000);
It suddenly works perfectly. What!??
So I have this code:
var bindAll;
bindAll = function ()
{
$('#somediv').bind('mouseover', function(){do something});
};
var init;
init = function ()
{
bindAll();
...
};
$(document).ready(init());
and it does not work. But if I put the bind on a timer by replacing:
bindAll();
with
tt = setTimeout('bindAll()', 1000);
It suddenly works perfectly. What!??
Share asked Jun 1, 2011 at 0:57 providenceprovidence 29.5k13 gold badges48 silver badges63 bronze badges 03 Answers
Reset to default 5jQuery ready
expects a handler, not a function call.
// $(document).ready(init()); <-- Not working
$(document).ready(init); <-- Working!
Working example at: http://jsfiddle/marcosfromero/Qwghb/
You aren't passing init to $(document).ready
, you're passing whatever init returns.
Try this:
$(document).ready(init);
Explanation:
When you were trying to pass the function, you were actually running it. At the time of you running it, the DOM wasn't ready, so the bindings to the element didn't take place, because it didn't exist.
When you did the timeout, it worked because it took less then one second for the DOM to finish, which means by the time it gets run, the element is there, so it can bind the event.
just pass init without parenthesis into the ready function. it is being executed immediately in your case and not on document ready:
$(document).ready(init);
本文标签: javascriptjquery bind() andor ready() not workingStack Overflow
版权声明:本文标题:javascript - jquery .bind() andor .ready() not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744651527a2617714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论