document open() 方法定义
open() 方法打开一个新文档,并擦除当前文档的内容。语法如下:
document.open(mimetype,replace)
| 参数 | 说明 |
|---|---|
| mimetype | 可选。设定即将写入的文档类型,默认值是 "text/html" |
| replace | 可选。当此参数设置后,可引起新文档从父文档继承历史条目。 |
提示:新文档用 document.write() 方法或 document.writeln() 方法写入内容,写入内容后,必须用 document.close() 方法关闭文档,并迫使其内容显示出来。
open() 方法实例
<html>
<head>
<script type="text/javascript">
function createNewDoc()
{
var new_doc = document.open("text/html","replace");
var txt = "<html><body>这是新的文档</body></html>";
new_doc.write(txt);
new_doc.close();
}
</script>
</head>
<body>
<button onclick="createNewDoc()">点击写入新文档</button>
</body>
</html>
运行该例子,点击按钮,页面内容会被替换。查看网页源代码,依然是原来的饿,但用 Chrome 等浏览器查看网页元素,已经是被替换后的内容了。
