我试图避免使用数据URI,因为我不希望生成的文档存储在浏览器历史记录中。是否可能原地替换整个HTML文档?
我尝试了jQuery("html").html("<html>....</html>")
,但style
信息无法保存。
我试图避免使用数据URI,因为我不希望生成的文档存储在浏览器历史记录中。是否可能原地替换整个HTML文档?
我尝试了jQuery("html").html("<html>....</html>")
,但style
信息无法保存。
你可能想要这样做:
jQuery("body").html("new content");
在替换 body
元素内容时,"new content"
最好只包含正常出现在 body
元素中的标记而不包括其他内容。这将替换 body
元素的内容,同时保留您在 head
中拥有的任何内容(如样式表信息)。如果您还想更新标题,可以使用 document.title = "new title";
。
编辑 我开始思考替换 html
元素内的 所有 内容是否可行,以及会发生什么。所以我做了这个:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
font-family: sans-serif;
font-weight: bold;
color: blue;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript'>
(function() {
$(document).ready(pageInit);
function pageInit() {
$('#btnChange').live('click', changePage);
$('#btnHiThere').live('click', sayHi);
}
function changePage() {
$('html').html(
"<head><\/head>" +
"<body>" +
"<input type='button' id='btnHiThere' value='Click for Alert'>" +
"<p>Note how this text now looks.<\/p>" +
"<\/body>"
);
}
function sayHi() {
alert("Hi there");
}
})();
</script>
</head>
<body>
<input type='button' id='btnHiThere' value='Click for Alert'>
<input type='button' id='btnChange' value='Change Page'>
<p>Note now this text current appears in a sans-serif, bold, blue font.</p>
</body>
</html>
结果非常有趣— 我最终得到的DOM结构完全没有head
或body
,只有html
及其内部的head
和body
后代。这可能是新内容中样式出现问题的原因。如果我直接设置innerHTML
,基本上会得到相同的结果(这可能是为什么它在jQuery中不起作用的原因;jQuery在可以使用innerHTML
时使用它,尽管在无法使用时非常复杂)。但是,如果通过document.createElement
和document.appendChild
显式创建head
和body
元素,它可以正常工作。
所有这些几乎肯定意味着这比它值得的更费力。
但是:请注意,更改head
和body
元素的内容似乎完全没问题:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
font-family: sans-serif;
font-weight: bold;
color: blue;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript'>
(function() {
$(document).ready(pageInit);
function pageInit() {
$('#btnChange').live('click', changePage);
$('#btnHiThere').live('click', sayHi);
}
function changePage() {
$('head').html(
"<style type='text/css'>\n" +
"body { color: green; }\n" +
"<\/style>\n"
);
$('body').html(
"<input type='button' id='btnHiThere' value='Click for Alert'>" +
"<p>Note how this text now looks.<\/p>"
);
}
function sayHi() {
alert("Hi there");
}
})();
</script>
</head>
<body>
<input type='button' id='btnHiThere' value='Click for Alert'>
<input type='button' id='btnChange' value='Change Page'>
<p>Note now this text current appears in a sans-serif, bold, blue font.</p>
</body>
</html>
如果你将要加载的“页面”分成头部和主体两个部分,那么你可以方便地直接更新其中一部分。
没有jQuery:
var newString = [
"<!doctype html>"
, "<html>"
, "<head>"
, "</head>"
, "<body>"
, "<h1>new content</h1>"
, "</body>"
, "</html>"
].join("");
document.getElementById('myIframeId').contentDocument.documentElement.outerHTML = newString;