Hướng dẫn appendchild javascript là gì

Trong bài này chúng ta sẽ học cách sử dụng hàm appendChild trong Javascript, đây là hàm dùng để bổ sung một DOM Node vào vị trí cuối cùng của một DOM Node khác.

Nội dung chính

  • 1. Thêm một thẻ HTML vào hàm appendChild
  • 2.Thiết lập nội dung html thông qua hàm createTextNode
  • Definition and Usage
  • Return Value
  • More Examples
  • Browser Support

Hướng dẫn appendchild javascript là gì

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Ví dụ, bạn có một thẻ div và bên trong thẻ div có 10 thẻ p. Nếu bạn muốn bổ sung một thẻ bất kì vào vị trí thứ 11 thì có thể sử dụng hàm appendChild .

Hàm này có một tham số truyền vào, đó phải là một DOM Node. Cú pháp như sau:

node.appendChild(node_append);

1. Thêm một thẻ HTML vào hàm appendChild

Giả sử mình có cấu trúc HTML như sau.

Bài viết này được đăng tại [free tuts .net]

Chào mừng các bạn đến với freetuts

Bây giờ mình muốn viết chương trình khi click vào nút button thì sẽ thêm một thẻ p có nội dung là "Học Javascript tại freetuts miễn phí" vào vị trí cuối cùng bên trong thẻ div.

Đây chính là hàm xử lý:

function addString(){
    var node = document.getElementById('wrapper');

    // Tạo một thẻ p theo yêu cầu
    var p = document.createElement('p');
    p.innerHTML = "Học Javascript tại freetuts miễn phí";

    // Thêm nó vào node
    node.appendChild(p);
}

2.Thiết lập nội dung html thông qua hàm createTextNode

Nếu bạn muốn bổ sung một đoạn text thì có thể sử dụng hàm createTextNode để tạo một text node. Text node bản chất vẫn là DOM Node, nhưng bản thân nó chỉ là một đoạn text chứ không phải một thẻ HTML.

Chào mừng các bạn đến với

Tóm lại, hàm appendChild trong js có công dụng là bổ sung một node vào vị trí cuối cùng bên trong một node khác. Nó có công dụng giống như phương thức append trong jQuery, điểm khác biệt duy nhất là đối với appendChild thì bắt buộc phải truyền vào một Dom Node, còn jQuery thì có thể là một Dom Node hoặc một chuỗi HTML.

Examples

Append an item to a list:

const node = document.createElement("li");
const textnode = document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);

Try it Yourself »

Move an item from one list to another:

const node = document.getElementById("myList2").lastElementChild;
document.getElementById("myList1").appendChild(node);

Before:

  • Coffee
  • Tea
  • Water
  • Milk

After:

  • Coffee
  • Tea
  • Milk
  • Water

Try it Yourself »

More examples below.


Definition and Usage

The appendChild() method appends a node (element) as the last child of an element.


Syntax

element.appendChild(node)

or

node.appendChild(node)

Parameters

Parameter Description
node Required.
The node to append.

Return Value

Type Description
Node The appended node.


More Examples

To create a paragraph with a text.

  • Create a paragraph element
  • Create a text node
  • Append the text node to the paragraph
  • Append the paragraph to the document.

Create a

element and append it to a

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

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 »


Browser Support

element.appendChild() is a DOM Level 1 (1998) feature.

It is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes 9-11 Yes Yes Yes Yes