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

后端 未结 12 1605
后悔当初
后悔当初 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:21

    I had such Problem, Just changed <script type="text/javascript"> to <script type="javascript"> and all problems gone!

    0 讨论(0)
  • 2020-12-10 01:23

    Just had this crop up and it turned out to be a spelling mistake in the script tag:

    <script type="text/javscript">

    Note the missing 'a' in javascript. Changing to simply:

    <script>

    Fixed the problem.

    0 讨论(0)
  • 2020-12-10 01:25

    Yes, i noticed that sometimes is problem with that, i wrote safe solution for that, which fix this problem.

    // jquery plugin 'jquery.ready.fix.js'
    // @author Szymon Działowski
    ;(function ($) {
        $.fn.ready.old || $(function (r) {
            r = $.fn.ready;
            $.fn.ready = function (fn) {    $.isReady ? fn() : r(fn);    };
            $.fn.ready.old = r;
        });
    })(jQuery);
    

    after that you can run code:

    $(function () {alert('go')})
    

    and it always fire alert.

    PS: it is a technique called "duck punching" more about: http://www.paulirish.com/2010/duck-punching-with-jquery/

    0 讨论(0)
  • 2020-12-10 01:26

    Another cause that will silently fail, and all remaining callbacks never called:

    $(document).ready(null);
    

    So check if you have variables or syntax errors that return null. Like this one:

    $(document).ready(function($){}(jQuery));
    

    Notice that the function above is called instantly and undefined is returned.

    0 讨论(0)
  • 2020-12-10 01:27

    Also look out how you define the jquery include tag. It seems for some reason you need to open and close the tag with two seperate tags. Using one tag with closing it seems not to work:

    Works:

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    

    Doesn't work:

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"/>
    
    0 讨论(0)
  • 2020-12-10 01:28

    Another point to check: Maybe you have a PHP error in your program. A PHP Fatal error exits the script and $(document).ready will never be executed.

    Depending on your server settings or on your CSS you might not see the Fatal error message.

    0 讨论(0)
提交回复
热议问题