Create a text node and append it to the document:
let textNode = document.createTextNode("Hello World"); document.body.appendChild(textNode);
Try it Yourself »
Create a <h1> element with a text node:
const h1 = document.createElement("h1");
const textNode = document.createTextNode("Hello World");
h1.appendChild(textNode);
Try it Yourself »
Create a <p> element with a text node:
const para = document.createElement("p");
const textNode = document.createTextNode("Hello World");
para.appendChild(textNode);
Try it Yourself »
All HTML elements are nodes.
Elements are nodes. Attributes are nodes. Texts are nodes.
Some elements contain other nodes.
Some elements contain text nodes.
Some elements consain attribute nodes.
The createTextNode()
method creates a text node.
document.createTextNode(
text)
Parameter | Description |
text | Required. The text for the node. |
Type | Description |
Node | The created text node. |
document.createTextNode()
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!