Document.createTextNode()

新しい Text ノードを生成します。このメソッドは HTML 文字をエスケープするのに利用できます。

構文

var text = document.createTextNode(data);
  • text: Text ノード。
  • data: テキストノードの中に入れるデータが入った文字列

html
<!doctype html>
<html lang="en">
  <head>
    <title>createTextNode example</title>
    <script>
      function addTextNode(text) {
        var newtext = document.createTextNode(text),
          p1 = document.getElementById("p1");

        p1.appendChild(newtext);
      }
    </script>
  </head>

  <body>
    <button onclick="addTextNode('YES! ');">YES!</button>
    <button onclick="addTextNode('NO! ');">NO!</button>
    <button onclick="addTextNode('WE CAN! ');">WE CAN!</button>

    <hr />

    <p id="p1">First line of paragraph.</p>
  </body>
</html>

仕様書

Specification
DOM Standard
# ref-for-dom-document-createtextnode①

ブラウザーの互換性

BCD tables only load in the browser