Node.appendChild()

There was a scripting error on this page. While it is being addressed by site editors, you can view partial content below.

This translation is incomplete. Please help translate this article from English.

{{ APIRef }}

The Node.appendChild() method adds a node to the end of the list of children of a specified parent node. If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position (i.e. there is no requirement to remove the node from its parent node before appending it to some other node).

This means that a node can't be in two points of the document simultaneously. So if the node already has a parent, it is first removed, then appended at the new position. The Node.cloneNode() can be used to make a copy of the node before appending it under the new parent. Note that the copies made with cloneNode will not be automatically kept in sync.

This method is not allowed to move nodes between different documents. If you want to append node from a different document the document.importNode() method must be used.

Cú pháp

var aChild = element.appendChild(aChild);

Giá trị trả về chính là node con vừa được thêm vào.

Ví dụ

// Create a new paragraph element, and append it to the end of the document body
var p = document.createElement("p");
document.body.appendChild(p);

Specifications

Specification Status Comment
DOM
The definition of 'Node.appendChild()' in that specification.
Living Standard No change from Document Object Model (DOM) Level 3 Core Specification.
Document Object Model (DOM) Level 3 Core Specification
The definition of 'Node.appendChild()' in that specification.
Recommendation No change from Document Object Model (DOM) Level 2 Core Specification.
Document Object Model (DOM) Level 2 Core Specification
The definition of 'Node.appendChild()' in that specification.
Recommendation No change from Document Object Model (DOM) Level 1 Specification.
Document Object Model (DOM) Level 1 Specification
The definition of 'Node.appendChild()' in that specification.
Recommendation Initial definition.

Trình duyệt hỗ trợ

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 1.0 (1.7 or earlier) (Yes) (Yes) (Yes)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) 1.0 (1.0) (Yes) (Yes) (Yes)

 

Xem thêm

Document Tags and Contributors

 Contributors to this page: nhvu1988
 Last updated by: nhvu1988,