Why is $(document).ready not firing for me?

后端 未结 12 1646
后悔当初
后悔当初 2020-12-10 00:26

In a php file i have used include to include the following js.php file and prior to that i have included the jquery file.



        
12条回答
  •  感动是毒
    2020-12-10 01:10

    I faced a similar problem too and this is what I did.

    Ensure that document.ready is placed after the callback function is defined in your javascript file.

    Here, init is not defined yet when document.ready is being parsed.

    //won't work
    $(document).ready(init);
    var init = function() {
       console.log('init not called');
    }
    

    Correct method:

    //Works
    var init = function() {
       console.log('document is ready');
    }
    $(document).ready(init);
    

    Notice that $(document).ready(init) is placed at the end.

提交回复
热议问题