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

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

提交回复
热议问题