aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/api/document/createelement/index.html
blob: 6cb461eeb55403600de49014e273cdf19324a057 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: Document.createElement()
slug: Web/API/Document/createElement
tags:
  - API
  - DOM
  - Document
  - Method
  - Reference
  - Web
translation_of: Web/API/Document/createElement
---
<div>{{APIRef("DOM")}}</div>

<p>HTML 문서에서, <strong><code>Document.createElement()</code></strong> 메서드는 지정한 <code>tagName</code>의 HTML 요소를 만들어 반환합니다. <code>tagName</code>을 인식할 수 없으면 {{domxref("HTMLUnknownElement")}}를 대신 반환합니다.</p>

<h2 id=".EB.AC.B8.EB.B2.95" name=".EB.AC.B8.EB.B2.95">구문</h2>

<pre class="eval"><var>let element</var> = <var>document</var>.createElement(<var>tagName[, options]</var>);</pre>

<h3 id="매개변수">매개변수</h3>

<dl>
 <dt><code>tagName</code></dt>
 <dd>생성할 요소의 유형을 가리키는 문자열.</dd>
 <dt><code>options</code> {{optional_inline}}</dt>
 <dd><code>is</code> 속성 하나를 가진 <code>ElementCreationOptions</code> 객체. 속성의 값은 <code>customElements.define()</code>을 사용해 정의한 사용자 정의 요소입니다.</dd>
</dl>

<h3 id="반환_값">반환 값</h3>

<p>새로운 {{domxref("Element")}}.</p>

<h2 id="예제" name="예제">예제</h2>

<p>아래 예제는 새로운 <code>&lt;div&gt;</code> 엘리먼트를 생성한 후, ID가 "div1" 인 요소 이전에 추가합니다.</p>

<h4 id="HTML">HTML</h4>

<pre class="brush: html"><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;||Working with elements||&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div id="div1"&gt;위의 텍스트는 동적으로 추가했습니다.&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>

<h4 id="JavaScript">JavaScript</h4>

<pre class="brush: js"><code>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);
}</code></pre>

<p>{{EmbedLiveSample("예제", 500, 50)}}</p>

<h2 id="명세">명세</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('DOM WHATWG', "#dom-document-createelement", "Document.createElement")}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="브라우저_호환성">브라우저 호환성</h2>

<p>{{Compat("api.Document.createElement")}}</p>

<h2 id="같이_보기">같이 보기</h2>

<ul>
 <li>{{domxref("Node.removeChild()")}}</li>
 <li>{{domxref("Node.replaceChild()")}}</li>
 <li>{{domxref("Node.appendChild()")}}</li>
 <li>{{domxref("Node.insertBefore()")}}</li>
 <li>{{domxref("Node.hasChildNodes()")}}</li>
 <li>{{domxref("document.createElementNS()")}} — to explicitly specify the namespace URI for the element.</li>
</ul>