Hướng dẫn dùng document createelement trong PHP

Hướng dẫn dùng document createelement trong PHP

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
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

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
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