Tài liệu HTML DOM createAttribution()
Ví dụ
// Create a class attribute:
const att = document.createAttribute("class");
// Set the value of the class attribute:
att.value = "democlass";
// Add the class attribute to the first h1:
const h1 = document.getElementsByTagName("H1")[0];
h1.setAttributeNode(att);
Hãy tự mình thử »
// Create a style attribute:
const att = document.createAttribute("style");
// Set the value of the style attribute:
att.value = "color:red";
// Add the style attribute to the first h1:
const h1 = document.getElementsByTagName("h1")[0];
h1.setAttributeNode(att);
Hãy tự mình thử »Thêm ví dụ dưới đây.
Sự miêu tả
Phương thức createAttribute()
tạo một thuộc tính và trả về thuộc tính đó dưới dạng đối tượng Attr.
Thay thế:
Sử dụng Phương thức setAttribution() sẽ dễ dàng hơn.
Xem thêm:
Cú pháp
document.createAttribute( name )
Thông số
Parameter | Description |
name | Required. The name of the attribute to create. |
Giá trị trả về
Kiểu | Sự miêu tả |
Nút | Nút thuộc tính đã tạo. |
Thêm ví dụ
Thêm thuộc tính href="www.example.com.vn" vào phần tử neo:
// Create a href attribute:
const att = document.createAttribute("href");
// Set the value of the href attribute:
att.value = "https://www.example.com.vn";
// Add the href attribute to an element:
element.setAttributeNode(att);
Hãy tự mình thử »Hỗ trợ trình duyệt
document.createAttribute()
là tính năng DOM Cấp 1 (1998).
Nó được hỗ trợ đầy đủ trong tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |