HTML - DOM Document open() 方法



HTML DOM document **open()** 方法打开一个文档以供写入。文档的所有现有内容都将被清除。

语法

document.open();

参数

此方法接受下面列出的两个参数,但这两个参数都是可选的。

参数 描述
type 它指定文档的类型。其默认值设置为“text/html”。
replace 它指定新文档的历史记录条目将替换文档的当前历史记录条目。

返回值

它返回文档对象的实例。

HTML DOM Document 'open()' 方法的示例

以下示例说明了 open() 方法以打开文档以供写入。

打开文档

在以下示例中,open() 方法用于打开、写入和关闭文档。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document open() Method
    </title>
</head>
<body>
    <button onclick="fun()">Click me</button>
    <script>
        function fun() {
            document.open();
            document.write("Welcome to Tutorials Point...");
            document.close();
        }
    </script>
</body>
</html>

在新窗口中打开文档

在以下示例中,open() 方法用于在新窗口中打开、写入和关闭文档。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document open() Method
    </title>
</head>
<body>
    <p>Click to open a document in new window.</p>
    <button onclick="fun()">Click me</button>
    <script>
        function fun() {
            let x = window.open()
            x.document.open();
            x.document.write("Welcome to Tutorials Point...");
            x.document.close();
        }
    </script>
</body>
</html>

支持的浏览器

方法 Chrome Edge Firefox Safari Opera
open() 是 64 是 12 是 1 是 11 是 51
html_dom_document_reference.htm
广告

© . All rights reserved.