English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM createAttribute() 메서드

HTML DOM Document 객체

createAttribute()메서드를 사용하여 새로운 속성 노드를 생성하고, 이 속성을Attr객체를 반환합니다.

DOM은 createAttribute()와 같은 방식으로 특정 요소에 추가할 속성 유형을 강제로 하지 않습니다.

사용attribute.value 속성을 사용하여 속성 값을 설정합니다.

사용element .setAttributeNode()메서드를 사용하여 새로운 속성을 요소에 추가할 수 있습니다.

또는, 다음과 같이 사용할 수 있습니다:element .setAttribute()createAttribute() 메서드 대신 메서드를 사용합니다.

문법:

document.createAttribute(name)
var node = document.getElementById("result");
var a = document.createAttribute("href");
a.value = "https:"//ko.oldtoolbag.com/";
node.setAttributeNode(a);
테스트를 보세요‹/›

브라우저 호환성

모든 브라우저에서 createAttribute() 메서드가 완전히 지원됩니다:

메서드
createAttribute()

파라미터 값

파라미터설명
이름속성 이름을 포함한 문자열

기술 세부 사항

반환 값:생성된 속성을 나타내는 Attr 객체
DOM 버전:DOM 등급1

更多实例

src 속성을 생성하여 값이 "clouds.png"인 것을 <img> 요소에 삽입합니다:

var node = document.querySelector("img");
var a = document.createAttribute("src");
a.value = "/run/images/clouds.png";
node.setAttributeNode(a);
테스트를 보세요‹/›

HTML DOM Document 객체