假设我有这个页面(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>
Run Code Online (Sandbox Code Playgroud)
另一页(page2.html):
<html>
<body>
<h1>BBB</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
正如您在我的代码段中所看到的,我想从page2.html获取HTML并使用获取的响应替换page.html的整个内容.
怎么做?
您可以使用document.open()创建新页面,document.write()来编写数据,然后使用document.close()来完成:
document.open("text/html");
document.write(data);
document.close();
Run Code Online (Sandbox Code Playgroud)
https://developer.mozilla.org/en/DOM/document.open