This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Document.createElement()
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
* Some parts of this feature may have varying levels of support.
HTML 문서에서,Document.createElement() 메서드는 지정한tagName의 HTML 요소를 만들어 반환합니다.tagName을 인식할 수 없으면HTMLUnknownElement를 대신 반환합니다.
In this article
구문
js
let element = document.createElement(tagName[, options]);매개변수
tagName생성할 요소의 유형을 가리키는 문자열.
optionsOptionalis속성 하나를 가진ElementCreationOptions객체. 속성의 값은customElements.define()을 사용해 정의한 사용자 정의 요소입니다.
반환 값
새로운Element.
예제
아래 예제는 새로운<div> 엘리먼트를 생성한 후, ID가 "div1" 인 요소 이전에 추가합니다.
HTML
html
<!doctype html><html> <head> <title>||Working with elements||</title> </head> <body> <div>위의 텍스트는 동적으로 추가했습니다.</div> </body></html>JavaScript
js
document.body.onload = addElement;function addElement() { // create a new div element var newDiv = document.createElement("div"); // and give it some content var newContent = document.createTextNode("환영합니다!"); // add the text node to the newly created div newDiv.appendChild(newContent); // add the newly created element and its content into the DOM var currentDiv = document.getElementById("div1"); document.body.insertBefore(newDiv, currentDiv);}명세
| Specification |
|---|
| DOM> # ref-for-dom-document-createelement①> |
브라우저 호환성
같이 보기
Node.removeChild()Node.replaceChild()Node.appendChild()Node.insertBefore()Node.hasChildNodes()document.createElementNS()— to explicitly specify the namespace URI for the element.