【问题标题】:Can I use jquery doucument ready in a loaded html / script subpage我可以在加载的 html / 脚本子页面中使用 jquery 文档吗
【发布时间】:2012-04-08 20:30:29
【问题描述】:

我正在使用多个表单的动态序列。每个表单(一个子页面)都使用 $( "#dyn-form" ).load( form-x.html ) 在主页面中动态加载。 每个 form-x.html 都嵌入了自己的脚本。此脚本必须在加载 form-x.html 后执行。 我从准备好的文档开始每个脚本,以确保将动态加载的内容加载到 dom 中。这有意义吗?

更新

感谢您的所有帮助。我可以通过在加载回调中调用加载的 page.html 的嵌入函数来解决它:window[page].apply();

function nextForm(page) {
    $("#dyn-form").load(page + '.html', function(response, status, xhr) {
        if (status == "error") {
            var msg = "Sorry but there was an error: ";
            $("#error").html(msg + xhr.status + " " + xhr.statusText);
        }
        else {          // load OK and finished then call embedded script
            window[page].apply();
        }
    });
}
$(document).ready(function(){
    nextForm('form-1');     // load the first form  
});

【问题讨论】:

    标签: jquery jquery-load


    【解决方案1】:

    如果你想在加载远程页面后执行一些脚本,你可以这样做

    $("#dyn-form" ).load("form-x.html",function(){
      //Script here will execute only after the load completed
    });
    

    【讨论】:

    • 第一部分不是真的。 DOM 就绪事件只触发一次!
    • 如果 dom 就绪只触发一次,我如何触发我的 form-x.html 中的脚本。我可以使用 .on(将事件绑定到函数)和 .trigger 来执行用户事件吗?我在加载回调中触发事件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2012-12-29
    • 2014-01-07
    • 2012-06-07
    • 1970-01-01
    相关资源
    最近更新 更多