简体   繁体   English

在div中加载html页面

[英]loading html page inside div

how can I load an html page inside a div. 如何在div中加载html页面。 With the 'object' tag it's loading, but I think it's not a good approach. 有了'object'标签,它正在加载,但我认为这不是一个好方法。 It is not an external file. 它不是外部文件。 Is dojo good for this? 道场对此有好处吗?

使用jquery

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

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 . 我不完全确定你在寻找什么,但如果你想在另一个HTML文档中显示HTML文档,那么我认为唯一的方法是使用iframe Check out this tutorial: http://www.designplace.org/tutorials.php?page=1&c_id=1 查看本教程: http//www.designplace.org/tutorials.php? page = 1& c_id = 1

Perhaps you could load the HTML document and strip away the HEAD and wrapping BODY elements and then insert the code into the DIV element. 也许您可以加载HTML文档并去除HEAD并包装BODY元素,然后将代码插入到DIV元素中。

EDIT: Ah, no iframes you said. 编辑:啊,没有你说的iframes Well, then I propose the latter. 那么,我提出后者。 ^^ ^^

No reason to use jQuery just to load content to your page (eg only to one div) if there is no another tasks which need framework's functions :) 如果没有其他需要框架功能的任务,没有理由只使用jQuery将内容加载到您的页面(例如只加载到一个div):)

This should be done with AJAX basics. 这应该通过AJAX基础来完成。 Check this out: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first 看看这个: http//www.w3schools.com/ajax/tryit.asp?filename = tryajax_first

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

You have to use jQuery with load() method. 你必须使用jQuery和load()方法。 Here jQuery reference about load method: http://api.jquery.com/load/ 这里有关于load方法的jQuery参考: http//api.jquery.com/load/

You can still use jquery to load the content through an ajax call. 您仍然可以使用jquery通过ajax调用加载内容。 Take the result and delete <body> and everything before it, and </body> and everything behind it. 取结果并删除<body>及其前面的所有内容, </body>及其后面的所有内容。 You can use regex to make sure you include any attributes of the body tag. 您可以使用正则表达式来确保包含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 :) 我的正则表达式有点生疏,所以你可能需要调整:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM