HTML DOM setAttributeNode() 메서드
❮ 요소 개체예시
<h1> 요소의 클래스 속성 노드를 설정합니다.
var h1 = document.getElementsByTagName("H1")[0]; // Get the first <h1> element in the document
var att = document.createAttribute("class"); // Create a "class" attribute
att.value = "democlass"; // Set the value of the class attribute
h1.setAttributeNode(att); // Add the class attribute to <h1>
속성 노드를 설정하기 전에:
Hello World
속성 노드를 설정한 후:
Hello World
아래에서 더 많은 "직접 사용해 보기" 예를 살펴보세요.
정의 및 사용
setAttributeNode() 메서드는 지정된 속성 노드를 요소에 추가합니다.
지정된 속성이 이미 존재하는 경우 이 메서드가 대체합니다.
이 메서드의 반환 값은 Attr 개체입니다. 자세한 내용 은 HTML DOM 속성 객체 를 참조하십시오 .
setAttribute() 메서드 도 참조하십시오 .
팁: removeAttributeNode() 메소드를 사용 하여 요소에서 속성 노드를 제거하십시오.
브라우저 지원
Method | |||||
---|---|---|---|---|---|
setAttributeNode() | Yes | Yes | Yes | Yes | Yes |
통사론
element.setAttributeNode(attributenode)
매개변수 값
Parameter | Type | Description |
---|---|---|
attributenode | Attr object | Required. The attribute node you want to add |
기술적 세부 사항
반환 값: | 대체된 속성 노드를 나타내는 Attr 개체(있는 경우), 그렇지 않으면 null |
---|---|
DOM 버전 | 핵심 레벨 1 요소 개체 |
더 많은 예
예시
<a> 요소의 href 속성 노드를 설정합니다.
var anchor = document.getElementById("myAnchor"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "https://www.w3schools.com"; // Set the value of the href attribute
anchor.setAttributeNode(att); // Add the href attribute to <a>
속성 노드를 설정하기 전에:
Go to w3schools.com
속성 노드를 설정한 후:
관련 페이지
HTML 튜토리얼: HTML 속성
HTML DOM 참조: HTML DOM 속성 개체
HTML DOM 참조: setAttribute() 메서드
HTML DOM 참조: document.createAttribute() 메서드
HTML DOM 참조: 속성 .value 속성
HTML DOM 참조: getAttributeNode() 메서드
HTML DOM 참조: removeAttributeNode() 메서드
❮ 요소 개체