HTML DOM close() 方法

Document 對象參考手冊 Document 對象

定義和用法

close() 方法用於關閉一個由 document.open 方法打開的輸出流,並顯示選定的數據。

語法

document.close()


流覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要流覽器都支持 close() 方法


實例

實例 1

打開一個輸出流,添加一些文本,然後關閉輸出流:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IT研修(xuhuhu.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("<b>Hello World!</b>");
w.document.close();
</script>

</body>
</html>



Document 對象參考手冊 Document 對象