Hướng dẫn create html element js

Hướng dẫn create html element js

Trong một tài liệu HTML, phương thức createElement() tạo ra phần tử HTML được chỉ định bởi tagName, hoặc một HTMLUnknownElement nếu tagName không được nhận ra.

Cú pháp

var element = document.createElement(tagName);

tagName:

Là thẻ HTML mà bạn muốn tạo mới. Không sử dụng tên đủ điều kiện (như “html: a“) với phương thức này. Khi được gọi, createElement() chuyển đổi tagName thành chữ thường trước khi tạo phần tử. Trong Firefox, Opera và Chrome, createElement(null) hoạt động như createElement("null").

Giá trị trả về

Trả về Element mới.

Ví dụ

HTML




    Working with elements


    
The text above has been created dynamically.

JavaScript

document.body.onload = addElement;

function addElement () { 
    // create a new div element 
    var newDiv = document.createElement("div"); 
    // and give it some content 
    var newContent = document.createTextNode("Hi there and greetings!"); 
    // add the text node to the newly created div
    newDiv.appendChild(newContent);  

    // add the newly created element and its content into the DOM 
    var currentDiv = document.getElementById("div1"); 
    document.body.insertBefore(newDiv, currentDiv); 
}

Kết quả

Tính tương thích của trình duyệt web

Trình duyệt trên máy tính

Trình duyệt Tương thích
Chrome
Edge
FireFox
Internet Eplorer
Opera
Safari

Trình duyệt trên thiết bị di động

Trình duyệt Tương thích
Android Webview
Chrome for Android
Edge Mobile
FireFox for Android
Opera
iOS Safari
Samsung Internet ?

Tham khảo:

  • Web APIs: Đối tượng Document
  • Web APIs: Đối tượng Node

Bạn có thể dùng JavaScript để tạo mới phần từ HTML sau đó chèn vào trang web. Đây là các cách giúp bạn tạo mới một phần tử HTML

document.createElement(tag_name) tạo ra phần tử có thẻ tag_name như a, p, div ...
element.cloneNode() Tạo ra một phần tử bằng cách nhân bản phần tử chỉ ra (element)
document.createTextNode(text) tạo phần tử tử văn bản text HTML
 var node     = document.createTextNode("Tạo ra một phần tử");

 var linknode = document.createElement("a");
     linknode.href="https://xuanthulab.net/";
     linknode.innerText="xuanthulab.net";

Ví dụ trên sẽ tạo ra một nút text, nhưng nó chưa hiện thị cho đến khi bạn gắn phần tử đó vào HTML document để nó là phần tử con của một phần tử nào đó, có một số cách để gắn phần tử tạo ra từ JavaScript vào DOM HTML

element.appendChild(newNode) Thêm phần tử newNode vào phần tử element nó trở thành phần tử con sau cùng của element
element.insertBefore(newNode, node2) Chèn phần tử newNode nằm phía trước node2
element.replaceChild(newNode, oldNode) Thay thế phần tử oldNode bằng phần tử newNode

Ví dụ sau tạo ra một phần tử đoạn văn p sau đó chèn nó vào phần tử div đang có sẵn trong DOM HTML

nội dung ví dụ

nội dung ví dụ

Loại bỏ phần tử

Để loại bỏ phần tử HTML, bạn chọn phần tử cha rồi sử dụng phương thức removeChild(node)

This is a paragraph.

This is another paragraph.

Ví dụ trên sẽ xóa bỏ phần tử đoạn văn thứ nhất

Bạn có thể sử dụng thủ thuật lấy thuộc tính parentNode để bỏ qua bước tìm phần tử cha trong DOM: child.parentNode.removeChild(child);

This is a paragraph.

This is another paragraph.

Thay thế phần tử

Để thay thể một phần tử bằng một phần tử khác dùng cú pháp element.replaceChild(newNode, oldNode). Trong đó element là nút cha

This is a paragraph.

This is another paragraph.