Can I call $(document).ready() to re-activate all on load event handlers?

前端 未结 10 1565
情书的邮戳
情书的邮戳 2020-12-15 15:37

Does anyone happen to know IF and HOW I could re-call all on-load event handlers? I\'m referencing some .js files that I DON\'T have control over, and these .js libraries do

相关标签:
10条回答
  • 2020-12-15 16:00

    You can trigger document.ready second time if you change entire body content:

    $('body').html($('body').html())
    
    0 讨论(0)
  • 2020-12-15 16:00

    I don't think that this can be done since jquery unbinds the ready event after it is executed. From the source:

    // Trigger any bound ready events
    if ( jQuery.fn.trigger ) {
        jQuery( document ).trigger( "ready" ).unbind( "ready" );
    }
    
    0 讨论(0)
  • 2020-12-15 16:04

    I think it is straight forward to just change the ready event to pjax success

    Change it from:

    $(document).ready(function() {
        // page load stuff
    });
    

    To:

    $(document).on('ready pjax:success', function() {
        // will fire on initial page load, and subsequent PJAX page loads
    });
    
    0 讨论(0)
  • 2020-12-15 16:09

    Or, try this:

    jQuery.extend ({
    
        document_ready: function (value) {
            $(document).ready (value);
            $(document).ajaxComplete (value);
        }/* document_ready */
    
    });
    

    And instead of defining a function by saying:

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

    say:

    jQuery.document_ready (function () { blah blah blah });
    

    Explanation:

    Any function loaded to "document_ready" will be automatically loaded into both "$(document).ready ()" and "$(document).ajaxComplete ()" and will fire under both circumstances.

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