javascript 试图在 jquery .load("myHTML.html") 上触发 body 'onLoad' 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10976070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:41:11  来源:igfitidea点击:

Trying to trigger body 'onLoad' event on jquery .load("myHTML.html")

javascriptjqueryajax

提问by Snedden27

I have an html which mostly builds itself on page load, it works perfectly when I run it independently , but I need to put this html inside another html's div, I do this using ajax/jquery's .load function

我有一个主要在页面加载时自行构建的 html,当我独立运行它时它可以完美运行,但我需要将此 html 放在另一个 html 的 div 中,我使用 ajax/jquery 的 .load 函数执行此操作

i.e $('#myDiv').load('myHtml.html')

IE $('#myDiv').load('myHtml.html')

but loading it this way in the div doesn't trigger the body onload event and the page is not properly build. Can you suggest how I can somehow trigger the body onload function or some other way to achieve the same

但是在 div 中以这种方式加载它不会触发主体 onload 事件并且页面没有正确构建。你能建议我如何以某种方式触发身体加载功能或其他方式来实现相同的功能吗?

采纳答案by Raab

place it inside $(document).ready()

把它放在里面 $(document).ready()

   $(document).ready(function(){

        $('#myDiv').load('myHtml.html');
   });

回答by scriptin

You can trigger any event manually: $(document.body).trigger('load')

您可以手动触发任何事件: $(document.body).trigger('load')

Here's how you do it after your html is loaded:

这是加载 html 后的操作方法:

$('#myDiv').load('myHtml.html', function () {
    $(document.body).trigger('load');
});