aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/api/globaleventhandlers/onclick/index.html
blob: 9b655c0421d607a6a034f3f9c8434bcc3dd9bf02 (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
---
title: GlobalEventHandlers.onclick
slug: Web/API/GlobalEventHandlers/onclick
tags:
  - API
  - Event Handler
  - HTML DOM
  - Property
  - Reference
translation_of: Web/API/GlobalEventHandlers/onclick
---
<div>{{apiref("HTML DOM")}}</div>

<p>{{domxref("GlobalEventHandlers")}} 믹스인<sup>mixin</sup>의 <code><strong>onclick</strong></code> 속성은 주어진 요소의 {{event("click")}} 이벤트를 처리하기 위한 {{domxref("EventHandler")}}입니다.</p>

<p><code>click</code> 이벤트는 사용자가 요소를 클릭할 때 {{event("mousedown")}}과 {{event("mouseup")}} 다음으로 발동합니다.</p>

<div class="note"><strong>참고:</strong> <code>click</code> 이벤트에 행동을 붙일 땐, 마우스나 터치 스크린을 사용하지 않는 사용자도 그 행동을 할 수 있도록 {{event("keydown")}}이벤트에도 똑같이 적용하는걸 고려해보세요.</div>

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

<pre class="syntaxbox">target.onclick = <var>functionRef</var>;
</pre>

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

<p><code>functionRef</code>는 함수 이름이거나 <a href="/ko/docs/Web/JavaScript/Reference/Operators/function">함수 표현식</a>으로, 유일한 매개변수로 {{domxref("MouseEvent")}} 객체를 받습니다. 함수 내에서 {{jsxref("Operators/this", "this")}}는 이벤트가 발동한 요소를 가리킵니다.</p>

<p>하나의 객체에는 하나의 <code>onclick</code> 처리기만 할당할 수 있습니다. 더 유연한{{domxref("EventTarget.addEventListener()")}} 메서드를 선호하는 편이 좋을 수도 있습니다.</p>

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

<p>다음 예제는 클릭 위치를 기록합니다.</p>

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

<pre class="brush: html">&lt;p&gt;Click anywhere in this example.&lt;/p&gt;
&lt;p id="log"&gt;&lt;/p&gt;</pre>

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

<pre class="brush: js">let log = document.getElementById('log');

document.onclick = inputChange;

function inputChange(e) {
  log.textContent = `Position: (${e.clientX}, ${e.clientY})`;
}</pre>

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

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

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

<table class="spectable 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','webappapis.html#handler-onclick','onclick')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

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

<div>


<p>{{Compat("api.GlobalEventHandlers.onclick")}}</p>
</div>

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

<ul>
 <li>{{event("click")}} 이벤트</li>
 <li>관련 이벤트 처리기
  <ul>
   <li>{{domxref("GlobalEventHandlers.onauxclick")}}</li>
   <li>{{domxref("GlobalEventHandlers.ondblclick")}}</li>
  </ul>
 </li>
</ul>