javascript - 如何用外部 HTML 替换整个 HTML?

标签 javascript jquery

假设我有这个页面(page.html):

<html>
    <body>
    <h1>AAA</h1>
<script type="text/javascript">
    //<![CDATA[
$(document).ready(function() {

 $.get('page2.html', function(data){
  // I want to replace the entire HTML with the HTML of page2.html
  // but this doesnt' work
  $('html').replaceWith(data);
 });

});    //]]>
</script>
    </body>
</html>

另一个页面(page2.html):

<html>
    <body>
    <h1>BBB</h1>
    </body>
</html>

正如您在我的代码片段中看到的,我想从 page2.html 中获取 HTML 并用获取的响应替换 page.html 的整个内容。

如何做到这一点?

最佳答案

您可以使用document.open()创建一个新页面,document.write()写入数据,然后document.close() 完成:

document.open("text/html");
document.write(data);
document.close();

https://developer.mozilla.org/en/DOM/document.open

关于javascript - 如何用外部 HTML 替换整个 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3699595/

相关文章:

javascript - GMAP3通过ajax get填充多个标记地址

javascript - jQuery 选择和过滤 div 内的元素

javascript - 将标题标签应用于 iFrame 中的框架

javascript - onclick 功能它仅在第二次单击后才起作用

javascript - 为 Js PDF 导入 autotable 插件时出错

javascript - Jquery 选择器使用脚本更新

javascript - 单独显示 json_encode 结果

javascript - Jqgrid hlink 用户格式

jquery - ajax jquery 错误 504 网关超时

javascript - TypeError : Router. use() 需要一个中间件函数,但在 Function.use 处得到一个字符串