aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/api/document/activeelement/index.html
blob: 2f4d4394d97f45752f0065c85bb789a94ff5b948 (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
---
title: DocumentOrShadowRoot.activeElement
slug: Web/API/DocumentOrShadowRoot/activeElement
tags:
  - API
  - Document
  - DocumentOrShadowRoot
  - Focus
  - Property
  - Reference
  - ShadowRoot
translation_of: Web/API/DocumentOrShadowRoot/activeElement
---
<div>{{APIRef("Shadow DOM")}}</div>

<p><span class="seoSummary">{{domxref("Document")}}{{domxref("ShadowRoot")}} 인터페이스의 <strong><code>activeElement</code></strong> 읽기 전용 속성은 DOM과 섀도우 DOM 내에서 현재 포커스를 받은 {{domxref("Element")}} 객체를 반환합니다.</span> 이 속성은 {{domxref("DocumentOrShadowRoot")}} {{Glossary("mixin", "믹스인")}}에서 상속받습니다.</p>

<p><code>activeElement</code> 접근 시점에 텍스트를 블록 선택하고 있는 경우 해당하는 {{htmlelement("input")}}이나 {{htmlelement("textarea")}} 객체를 반환하는데, 그러면 그 객체의 {{domxref("Document.selectionStart", "selectionStart")}}{{domxref("Document.selectionEnd", "selectionEnd")}} 메서드를 사용해 선택에 대한 더 자세한 정보를 알아낼 수 있습니다. 포커스가 자주 가는 다른 경우로는 {{htmlelement("select")}} 요소나 <code>type</code><code>"button"</code>, <code>"checkbox"</code>, <code>"radio"</code>{{htmlelement("input")}} 요소가 있습니다.</p>

<p>보통 사용자는 포커스 가능한 요소를 Tab 키를 사용해 탐색할 수 있고, 스페이스 바를 사용해 활성화(버튼을 누르거나 라디오 버튼을 켜는 등)할 수 있습니다. 포커스 가능한 요소는 현재 플랫폼과 브라우저 설정에 따라 다릅니다. 가령 macOS의 경우, 기본값에서는 텍스트 입력 칸이 아니면 보통 포커스 할 수 없습니다.</p>

<div class="note">
<p><strong>참고:</strong> 포커스(사용자의 입력 이벤트를 받는 요소)와 선택(문서 내에서 강조하고 있는 부분)은 다릅니다. 현재 선택 영역은 {{domxref("window.getSelection()")}}을 사용해 가져올 수 있습니다.</p>
</div>

<h2 id="구문">구문</h2>

<pre class="syntaxbox notranslate"><em>element</em> = <em>DocumentOrShadowRoot</em>.activeElement</pre>

<h3 id="값"></h3>

<p>포커스를 갖고 있는 {{domxref("Element")}}. 그런 요소가 없으면 {{htmlelement("body")}} 또는 {{jsxref("null")}}.</p>

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

<h3 id="HTML">HTML</h3>

<pre class="brush: html notranslate">&lt;p&gt;아래 두 영역에서 텍스트를 선택해보세요.&lt;/p&gt;

&lt;form&gt;
  &lt;textarea name="ta-example-one" id="ta-example-one" rows="7" cols="40"&gt;텍스트 영역 1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tincidunt, lorem a porttitor molestie, odio nibh iaculis libero, et accumsan nunc orci eu dui.&lt;/textarea&gt;
  &lt;textarea name="ta-example-two" id="ta-example-two" rows="7" cols="40"&gt;텍스트 영역 2. Fusce ullamcorper, nisl ac porttitor adipiscing, urna orci egestas libero, ut accumsan orci lacus laoreet diam. Morbi sed euismod diam.&lt;/textarea&gt;
&lt;/form&gt;

&lt;p&gt;활성화된 요소 ID: &lt;b id="output-element"&gt;&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;선택한 텍스트: &lt;b id="output-text"&gt;&lt;/b&gt;&lt;/p&gt;</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js notranslate">function onMouseUp(e) {
  const activeTextarea = document.activeElement;
  const selection = activeTextarea.value.substring(
    activeTextarea.selectionStart, activeTextarea.selectionEnd
  );

  const outputElement = document.getElementById('output-element');
  const outputText = document.getElementById('output-text');
  outputElement.innerHTML = activeTextarea.id;
  outputText.innerHTML = selection;
}

const textarea1 = document.getElementById('ta-example-one');
const textarea2 = document.getElementById('ta-example-two');
textarea1.addEventListener('mouseup', onMouseUp, false);
textarea2.addEventListener('mouseup', onMouseUp, false);</pre>

<h3 id="결과">결과</h3>

<p>{{ EmbedLiveSample('예제', '400', '400') }}</p>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'interaction.html#dom-document-activeelement', 'activeElement')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

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

<div>


<p>{{Compat("api.DocumentOrShadowRoot.activeElement")}}</p>
</div>