Append an item to a list:
const node = document.createElement("li");
const textnode = document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
Move an item from one list to another:
const node = document.getElementById("myList2").lastElementChild;
document.getElementById("myList1").appendChild(node);
More examples below.
The appendChild()
method appends a node (element) as the last child of an element.
element.appendChild(
node)
node.appendChild(
node)
Parameter | Description |
node | Required. The node to append. |
Type | Description |
Node | The appended node. |
To create a paragraph with a text.
Create a <p> element and append it to a <div> element:
const para = document.createElement("p");
const node = document.createTextNode("This is a paragraph.");
para.appendChild(node);
document.getElementById("myDIV").appendChild(para);
Try it Yourself »
Create a <p> element and append it to the document's body:
const para = document.createElement("P");
const node = document.createTextNode("This is a paragraph.");
para.appendChild(node);
document.body.appendChild(para);
Try it Yourself »
element.appendChild()
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 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!