Document:createTextNode() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015 .

创建新的 文本 节点。该方法可用于转义 HTML 字符。

语法

js
createTextNode(data)

包含要放入文本节点的数据的字符串。

返回值

文本 节点。

示例

html
<!doctype html>
<html lang="zh-CN">
    <title>createTextNode 示例</title>
    <script>
      function addTextNode(text) {
        const newtext = document.createTextNode(text);
        const p1 = document.getElementById("p1");
        p1.appendChild(newtext);
    </script>
  </head>
    <button onclick="addTextNode('是!');">是!</button>
    <button onclick="addTextNode('否!');">否!</button>
    <button onclick="addTextNode('我们可以!');">我们可以!</button>