jquery .bind() and/or .ready() not working

后端 未结 3 1312
攒了一身酷
攒了一身酷 2021-01-21 15:47

So I have this code:

var bindAll;
bindAll = function ()
{
    $(\'#somediv\').bind(\'mouseover\', function(){do something});
};

var init;
init = function ()
{
          


        
3条回答
  •  孤城傲影
    2021-01-21 16:27

    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.

提交回复
热议问题