Javascript 在div中加载html页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3564356/
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-08-23 05:12:57  来源:igfitidea点击:

loading html page inside div

javascripthtmldojo

提问by coder247

how can I load an html page inside a div. With the 'object' tag it's loading, but I think it's not a good approach. It is not an external file. Is dojo good for this?

如何在 div 中加载 html 页面。使用'object'标签加载,但我认为这不是一个好方法。它不是外部文件。道场对这个有好处吗?

采纳答案by coder247

This also works... using dojo...

这也有效...使用道场...

<script type="text/javascript">
var url = dojo.moduleUrl("dijit.form", "help.html");
dojo.xhrGet({
  url: url,
  load: function(html){
       dojo.byId("mycontent").innerHTML = html;
  }
});
</script>

<div id="mycontent">

</div>

Update:

更新:

In Dojo 1.7+, use require.toUrl instead of dojo.moduleUrl

在 Dojo 1.7+ 中,使用 require.toUrl 而不是 dojo.moduleUrl

回答by Starx

Use jquery

使用jQuery

$("#mydiv").load("myexternalfile.html");

回答by gablin

I'm not completely sure what you're looking for, but if you're wishing to display an HTML document inside another HTML document then I think the only way to do this is to use an iframe. Check out this tutorial: http://www.designplace.org/tutorials.php?page=1&c_id=1

我不完全确定您在寻找什么,但是如果您希望在另一个 HTML 文档中显示一个 HTML 文档,那么我认为唯一的方法是使用iframe. 查看本教程:http: //www.designplace.org/tutorials.php?page=1& c_id=1

Perhaps you could load the HTML document and strip away the HEADand wrapping BODYelements and then insert the code into the DIVelement.

也许您可以加载 HTML 文档并去除HEAD和 包装BODY元素,然后将代码插入到DIV元素中。

EDIT: Ah, no iframesyou said. Well, then I propose the latter. ^^

编辑:啊,不,iframes你说。那么我建议后者。^^

回答by Konstantin Likhter

No reason to use jQuery just to load content to your page (e.g. only to one div) if there is no another tasks which need framework's functions :)

如果没有其他需要框架功能的任务,则没有理由仅使用 jQuery 将内容加载到您的页面(例如,仅加载到一个 div):)

This should be done with AJAX basics. Check this out: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

这应该使用 AJAX 基础知识来完成。看看这个:http: //www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

回答by Joeri Hendrickx

You can still use jquery to load the content through an ajax call. Take the result and delete <body>and everything before it, and </body>and everything behind it. You can use regex to make sure you include any attributes of the body tag.

您仍然可以使用 jquery 通过 ajax 调用加载内容。取结果并删除<body>它之前的</body>所有内容以及它后面的所有内容。您可以使用正则表达式来确保包含 body 标签的任何属性。

Then you're left with the raw body html, which you can add to the div using jQuery.

然后剩下原始正文 html,您可以使用 jQuery 将其添加到 div。

jQuery.ajax({
    url: 'page.html', 
    success: function(data, textStatus, XMLHttpRequest) {
        data = data.replace(/.*<body.*?>/gi,'');
        data = data.replace(/</body>.*/gi,'');
        jQuery('#myDiv').html(data);
    }
});

My regex is a bit rusty so you might have to tweak that :)

我的正则表达式有点生疏,所以你可能需要调整它:)

回答by Nicola Celiento

You have to use jQuery with load() method. Here jQuery reference about load method: http://api.jquery.com/load/

您必须将 jQuery 与 load() 方法一起使用。这里关于加载方法的jQuery参考:http: //api.jquery.com/load/