HTML DOM 문서 createAttribute()
예
// 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);
// 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);
아래에 더 많은 예가 있습니다.
정의 및 사용
이 createAttribute()
메서드는 속성을 만들고 속성을 Attr 개체로 반환합니다.
통사론
document.createAttribute(name)
매개변수
Parameter | Description |
name | Required. The name of the attribute to create. |
반환 값
유형 | 설명 |
마디 | 생성된 속성 노드입니다. |
더 많은 예
앵커 요소에 href="www.w3schools.com" 속성을 추가합니다.
// Create a href attribute:
const att = document.createAttribute("href");
// Set the value of the href attribute:
att.value = "https://www.w3schools.com";
// Add the href attribute to an element:
element.setAttributeNode(att);
브라우저 지원
document.createAttribute()
DOM 레벨 1(1998) 기능입니다.
모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |