HTML DOM open() 方法

Document 对象参考手册 Document 对象

定义和用法

open() 方法打开一个输出流来收集 document.write()document.writeln() 方法输出的内容。

调用 open() 方法打开一个新文档并且用 write() 方法设置文档内容后,必须记住用 document.close() 方法关闭文档,并迫使其内容显示出来。

注意:如果目标文件已经存在,它将被清除。如果这个方法没有参数,会显示一个新窗口(about:blank)。

语法

document.open(MIMEtype,replace)

参数描述
MIMEtype可选。规定正在写的文档的类型。默认值是 "text/html"。
replace可选。当此参数设置后,可引起新文档从父文档继承历史条目。


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 open() 方法


实例

实例

打开一个输出流并添加文本,然后关闭输出流:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本站教程(icodebang.com)</title>
<script>
function createDoc(){
    var doc=document.open("text/html","replace");
    var txt="<!DOCTYPE html><html><body>学习 HTML DOM 很有趣!</body></html>";
    doc.write(txt);
    doc.close();
}
</script>
</head>

<body>
<input type="button" value="新文档" onclick="createDoc()">
</body>
</html>

尝试一下 »

实例 2

打开一个输出流 (一个新窗口; about:blank),并添加文本,然后关闭输出流:

<html>
<body>

<script>
var w=window.open();
w.document.open();
w.document.write("<h1>Hello World!</h1>");
w.document.close();
</script>

</body>
</html>

尝试一下 »


Document 对象参考手册 Document 对象

0 个评论

要回复文章请先登录注册