簡體   English   中英

AJAX頁面建立后觸發$ {document).ready

[英]Fire $(document).ready after AJAX page builds

我有以下代碼,一旦從URL加載頁面,它就會使用jquery動態更新頁面

$(document).ready(function() {
        var htmlattri="background-color:red;";
        $("body").attr("style", htmlattri);
        $("#navbar").append('<div id="test" style="position: absolute; left: 0px; top: 0px; z-index: 3000; height: 20px"><input type="button" value="Back" onclick="window.history.go(-1)" /></div>')
    });

我的問題是#navbar元素是通過ajax(非JQuery)構建的,它在$(document).ready被觸發后發生。

頁面完全構建后,是否有辦法觸發該事件?

我無法控制頁面的源代碼。

建立#navbar之后,將要嘗試執行的任何代碼放入ajax調用的success回調中。 您不能“移動” $(document).ready()調用,它將在創建dom之后始終執行

$ .Event怎么樣?

$('body').on('wearedone', function(){
    console.log('hello!');
});

$(document).ready(function() {
    var htmlattri="background-color:red;";
    $("body").attr("style", htmlattri);
    $("#navbar").append('<div id="test" style="position: absolute; left: 0px; top: 0px; z-index: 3000; height: 20px"><input type="button" value="Back" onclick="window.history.go(-1)" /></div>');

    // this is where we "reimplement" $(document).ready
    $('body').trigger($.Event('wearedone'));
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM