HTML - DOM 文件 createTextNode() 方法



HTML DOM 文件 **createTextNode()** 方法用於建立具有指定文字的文字節點。

語法

document.createTextNode(text);

引數

此方法接受如下所示的單個引數。

引數 描述
文字 它表示您想要新增到文件中的文字。

返回值

它返回建立的文字節點。

HTML DOM 文件“createTextNode()”方法示例

以下示例說明了 createTextNode() 方法的使用。

建立文字節點

在以下示例中,我們建立了一個文字節點並將其附加到主體。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document createTextNode() Method
    </title>
</head>
<body>
    <h3>HTML DOM document createTextNode() Method</h3>
    <button onclick="fun()">Create Text Node</button>
    <script>
       function fun() {
        let x = document.createTextNode("A simple text node is created..");
        document.body.appendChild(x);
       }
    </script>
</body>
</html>

建立段落元素

在以下示例中,我們建立了一個文字節點並將其附加到主體。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document createTextNode() Method
    </title>
</head>
<body>
    <h3>HTML DOM document createTextNode() Method</h3>
    <button onclick="fun()">Create Text Node</button>
    <script>
       function fun() {
        let h=document.createElement("h1");
        let x = document.createTextNode("A simple text node is created..");
        h.appendChild(x);
        document.body.appendChild(h);
       }
    </script>
</body>
</html>

支援的瀏覽器

方法 Chrome Edge Firefox Safari Opera
createTextNode() 是 1 是 12 是 1 是 1 是 7
html_dom_document_reference.htm
廣告