JavaScript createTextNodeでテキストノードを追加する

環境
Google Chrome 115.0.5790.171(Official Build) (64 ビット)
Windows 11 Pro 64bit

構文
document.createTextNode('テキスト’);
createTextNodeを使用して、テキストノードを追加します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<p id="cft">テキスト</p>
<button onclick="funA()">追加</button>
<script>
function funA() {
const strA = document.createTextNode('test data');
document.getElementById('cft').appendChild(strA);
}
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <p id="cft">テキスト</p> <button onclick="funA()">追加</button> <script> function funA() { const strA = document.createTextNode('test data'); document.getElementById('cft').appendChild(strA); } </script> </body> </html>
<!DOCTYPE html>
<html>
<body>

<p id="cft">テキスト</p>

<button onclick="funA()">追加</button>

<script>
function funA() {

const strA = document.createTextNode('test data');
document.getElementById('cft').appendChild(strA);

}
</script>

</body>
</html>

結果
指定した要素にテキストが追加されていることが確認します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<p id="cft">テキストtest data</p>
<p id="cft">テキストtest data</p>
<p id="cft">テキストtest data</p>

 

JavaScript

Posted by arkgame