HTML DOM createTextNode() method


HTML DOM createTextNode() Method

Instance

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p id="demo">单击按钮创建文本节点。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var t=document.createTextNode("Hello World");
	document.body.appendChild(t);
};

</script>

</body>
</html>

Run Instance»

Click the "Run Example" button to view the online example

HTML elements are usually composed of element nodes and text points.

To create a heading (H1), you must create the "H1" element and text node:

Example

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>

<p id="demo">单击按钮创建一个标题</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
	var h=document.createElement("H1");
	var t=document.createTextNode("Hello World");
	h.appendChild(t);
	document.body.appendChild(h);
};

</script>

</body>
</html>

Run Example»

Click the "Run Example" button to view the online example


Definition and usage

createTextNode() can create text nodes.


Browser Support

QQ截图20161108165429.png

All major browsers support the createTextNode() method


Syntax

document.createTextNode(text)

Parameters

##Parameters TypeDescriptionStringRequired. The text of the text node.
text
Return value

TypeDescriptionText Node ObjectCreate Text Node
Technical Details

DOM VersionCore Level 1 Document Object