繁体   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