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
<!DOCTYPE html> <html> <head> <title>Working with elements</title> </head> <body> <div id="div1">The text above has been created dynamically.</div> </body> </html>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 | Có |
Edge | Có |
FireFox | Có |
Internet Eplorer | Có |
Opera | Có |
Safari | Có |
Trình duyệt trên thiết bị di động
Trình duyệt | Tương thích |
Android Webview | Có |
Chrome for Android | Có |
Edge Mobile | Có |
FireFox for Android | Có |
Opera | Có |
iOS Safari | Có |
Samsung Internet | ? |
Tham khảo:
- Web APIs: Đối tượng Document
- Web APIs: Đối tượng Node
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. 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ềCú pháp
var element = document.createElement(tagName);
Trả về Element mới.
Ví dụ
HTML
<!DOCTYPE html> <html> <head> <title>Working with elements</title> </head> <body> <div id="div1">The text above has been created dynamically.</div> </body> </html>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 | Có |
Edge | Có |
FireFox | Có |
Internet Eplorer | Có |
Opera | Có |
Safari | Có |
Trình duyệt trên thiết bị di động
Trình duyệt | Tương thích |
Android Webview | Có |
Chrome for Android | Có |
Edge Mobile | Có |
FireFox for Android | Có |
Opera | Có |
iOS Safari | Có |
Samsung Internet | ? |
Tham khảo:
- Web APIs: Đối tượng Document
- Web APIs: Đối tượng Node