假设我有这个页面(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();
关于javascript - 如何用外部 HTML 替换整个 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3699595/