How to trigger $().ready() in jQuery?

前端 未结 10 1029
谎友^
谎友^ 2020-11-30 12:49

Using jQuery, is there a way to retrigger the document.ready trigger at some point after a page has loaded?

Update: jQuery sheds them once they are run.

10条回答
  •  有刺的猬
    2020-11-30 13:19

    In some cases you need to add more code (scripts) in another places of the code, so creating a function limit you to write all the code in the same area. This why I prefer to add my custom events, and then trigger them:

    //Add a page Ready custom event
    $( document ).on( "page.ready", function( event ) {
        //Your Ready Scripts
    });
    
    //Add a page Load custom event
    $( document ).on( "page.load", function( event ) {
        //Your Load Scripts
    });
    
    //Trigger Ready scripts on page ready
    jQuery(function($) {
        $( document ).trigger( "page.ready" );
    });
    
    //Trigger Load scripts on page load
    $(window).load(function () {
        $( document ).trigger( "page.load" );
    });
    
    //Trigger the Custom events manually
    $( document ).trigger( "page.ready" );
    $( document ).trigger( "page.load" );
    

    https://learn.jquery.com/events/introduction-to-custom-events/

提交回复
热议问题