Show Trong một tài liệu HTML, phương thức Cú phápvar element = document.createElement(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ư “ Giá trị trả về Trả về 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 webTrình duyệt trên máy tính
Trình duyệt trên thiết bị di động
Tham khảo:
Trong một tài liệu HTML, phương thức Cú phápvar element = document.createElement(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ư “ Giá trị trả về Trả về 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 webTrình duyệt trên máy tính
Trình duyệt trên thiết bị di động
Tham khảo:
|