HTML DOM Element setAttributeNode() 메서드

정의와 사용법

setAttributeNode() 메서드는 지정된 속성 노드를 요소에 추가하고 Attribute 객체를 반환합니다.

이 지정된 속성이 이미 존재하는 경우 이 메서드는 그것을 대체합니다.

대체 방안:

사용 setAttribute() 메서드 더 쉬웠습니다.

다른 것을 참조하십시오:

참고서:

getAttributeNode() 메서드

removeAttributeNode() 메서드

createAttribute() 메서드

getAttribute() 메서드

setAttribute() 메서드

hasAttribute() 메서드

Attribute value 속성

HTML DOM Attribute 객체

튜토리얼:

HTML 속성

setAttribute()과 setAttributeNode()의 차이

setAttribute() 메서드는 속성 값을 대체합니다.

setAttributeNode() 메서드는 Attribute 객체를 대체합니다.

속성을 요소에 추가하기 전에 Attr 객체를 생성하고 Attr 값을 설정해야 합니다.

결과는 같을 것입니다.

인스턴스

예제 1

초기화된 첫 번째 <h1> 요소의 class 속성 노드를 설정합니다:

const attr = document.createAttribute("class");
attr.value = "democlass";
const h1 = document.getElementsByTagName("H1")[0];
h1.setAttributeNode(attr);

직접 테스트해 보세요

설정 전:

Hello World

설정 후:

Hello World

예제 2

<a> 요소의 href 속성 노드를 설정하다:

const attr = document.createAttribute("href");
attr.value = "";
const anchor = document.getElementById("myAnchor");
anchor.setAttributeNode(attr);

직접 테스트해 보세요

설정 전:

codew3c.com을 방문하세요

설정 후:

codew3c.com을 방문하세요

문법

element.setAttributeNode(newAttr)

매개변수

매개변수 설명
newAttr 필수입니다. 추가하려는 속성 또는 속성 값이 변경되어야 하는 속성의 Attr 노드를 나타냅니다.

반환 값

타입 설명
객체

대체된 속성 노드의 Attr 객체를 나타냅니다.

속성이 대체되지 않았다면 null입니다.

throw

이 메서드는 다음 코드를 포함한 DOMException 예외를 퍼트립니다:

예외 설명
INUSE_ATTRIBUTE_ERR newAttr 기타 Element 노드의 속성 집합의 구성원입니다.
NO_MODIFICATION_ALLOWED_ERR 현재 Element 노드는 읽기 전용이며 속성을 수정할 수 없습니다.
WRONG_DOCUMENT_ERR newAttr 의 ownerDocument 속성은 설정하려는 Element 노드와 다릅니다.

브라우저 지원

element.setAttributeNode() DOM Level 1 (1998) 기능입니다.

모든 브라우저가 완전히 지원합니다:

크롬 IE 에지 파이어폭스 사파리 오페라
크롬 IE 에지 파이어폭스 사파리 오페라
지원 9-11 지원 지원 지원 지원