์ƒˆ์†Œ์‹

Languages/Java Script

[Js] ์ด๋ฒคํŠธ | ๋“ฑ๋ก๋ฐฉ๋ฒ• & inline | ํ”„๋กœํผํ‹ฐ ๋ฆฌ์Šค๋„ˆ

  • -
๋ฐ˜์‘ํ˜•

๋ชจ๋“  ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ [์ƒํ™œ์ฝ”๋”ฉ] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์˜์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "egoing Lee"๋‹˜์˜ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. 

์ƒ์—…์ ์ธ ์˜๋„๊ฐ€ ์•„๋‹Œ ๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•ด๋†“๋Š” ๋ชฉ์ ์œผ๋กœ ํฌ์ŠคํŒ… ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

www.inflearn.com/course/javascript-%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EA%B0%95%EC%A2%8C#

 

์›น๋ธŒ๋ผ์šฐ์ € Javascript (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ) - ์ธํ”„๋Ÿฐ

์ƒํ™œ์ฝ”๋”ฉ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ ์ž…๋‹ˆ๋‹ค. ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ง์ ‘ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ณต๋ถ€ํ•ด ๋ด…๋‹ˆ๋‹ค. ์ดˆ๊ธ‰ ์›น ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด JavaScript ์˜จ๋ผ์ธ ๊ฐ•์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ

www.inflearn.com


์ด๋ฒคํŠธ

 

์ด๋ฒคํŠธ๋ž€?

์ด๋ฒคํŠธ(event)๋Š” ์–ด๋–ค ์‚ฌ๊ฑด์„ ์˜๋ฏธํ•œ๋‹ค. ๋ธŒ๋ผ์šฐ์ €์—์„œ์˜ ์‚ฌ๊ฑด์ด๋ž€ ์‚ฌ์šฉ์ž๊ฐ€ ํด๋ฆญ์„ ํ–ˆ์„ '๋•Œ', ์Šคํฌ๋กค์„ ํ–ˆ์„ '๋•Œ', ํ•„๋“œ์˜ ๋‚ด์šฉ์„ ๋ฐ”๊พธ์—ˆ์„ '๋•Œ'์™€ ๊ฐ™์€ ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. 

<!DOCTYPE html>
<html>
<body>
    <input type="button" onclick="alert(window.location)" value="alert(window.href)" />
    <input type="button" onclick="window.open('bom.html')" value="window.open('bom.html')" />
</body>
</html>

onclick ์†์„ฑ์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ(alert(window.location))๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ด ๋ฒ„ํŠผ์„ ํด๋ฆญ ํ–ˆ์„ '๋•Œ' ์‹คํ–‰๋œ๋‹ค. ์ฆ‰ js ๊ฐœ๋ฐœ์ž๋Š” ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹คํ–‰ ๋˜์–ด์•ผ ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋“ฑ๋กํ•˜๊ณ , ๋ธŒ๋ผ์šฐ์ €๋Š” ๊ทธ ์ผ์ด ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋“ฑ๋ก๋œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์„ ์ด๋ฒคํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

event target

target์€ ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚  ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋Œ€์ƒ์„ ์˜๋ฏธํ•œ๋‹ค.์•„๋ž˜ ์ฝ”๋“œ์—์„œ ํƒ€๊ฒŸ์€ ๋ฒ„ํŠผ ํƒœ๊ทธ์— ๋Œ€ํ•œ ๊ฐ์ฒด๊ฐ€ ๋œ๋‹ค.

<input type="button" onclick="alert(window.location)" value="alert(window.href)" />

 

event type

์ด๋ฒคํŠธ์˜ ์ข…๋ฅ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” click์ด ์ด๋ฒคํŠธ ํƒ€์ž…์ด๋‹ค. ๊ทธ ์™ธ์—๋„ scroll์€ ์‚ฌ์šฉ์ž๊ฐ€ ์Šคํฌ๋กค์„ ์›€์ง์˜€๋‹ค๋Š” ์ด๋ฒคํŠธ์ด๊ณ , mousemove๋Š” ๋งˆ์šฐ์Šค๊ฐ€ ์›€์ง์˜€์„ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ์ด๋‹ค.

์ด๋ฒคํŠธ์˜ ์ข…๋ฅ˜๋Š” ์ด๋ฏธ ์•ฝ์†๋˜์–ด ์žˆ๋‹ค. ์•„๋ž˜ ๋งํฌ๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง€์›ํ•˜๋Š” ์ด๋ฒคํŠธ์˜ ์ข…๋ฅ˜๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

https://developer.mozilla.org/en-US/docs/Web/Reference/Events

 

Event reference | MDN

DOM Events are sent to notify code of interesting things that have taken place. Each event is represented by an object which is based on the Event interface, and may have additional custom fields and/or functions used to get additional information about wh

developer.mozilla.org

<input type="text" onchange="alert(this.value)" />

์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ change ์ฆ‰ ํ…์ŠคํŠธ ๋‚ด์šฉ์ด ๋ฐ”๋€๋‹ค๋ฉด ๊ฒฝ๊ณ ๋ฅผ ๋„์šฐ๋Š” ๊ฒฝ์šฐ change๊ฐ€ ๋ฐ”๋กœ ์ด๋ฒคํŠธ ํƒ€์ž…์ธ ๊ฒƒ์ด๋‹ค.

 

event handler

์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์—์„œ๋Š” alert(window.location)  ๊ฒฝ๊ณ ์ฐฝ์„ ์˜๋ฏธํ•œ๋‹ค.

์ด๋ฒคํŠธ ํƒ€์ž…์˜ ๋“ค์–ด๊ฐ€๋Š” ์†์„ฑ์ด ๋ฐ”๋กœ ํ•ธ๋“ค๋Ÿฌ ์ธ ๊ฒƒ์ด๋‹ค.

 


inline

์ธ๋ผ์ธ(inline) ๋ฐฉ์‹์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž. ์ธ๋ผ์ธ ๋ฐฉ์‹์€ ์ด๋ฒคํŠธ๋ฅผ ์ด๋ฒคํŠธ ๋Œ€์ƒ์˜ ํƒœ๊ทธ ์†์„ฑ์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์œ„์—์„œ ์•Œ์•„๋ณด์•˜๋˜ ๋ฐฉ์‹๋“ค์ด inline ๋ฐฉ์‹์ด๋‹ค. ํƒœ๊ทธ์•ˆ์— ์ด๋ฒคํŠธ๊ฐ€ ์†์„ฑ์œผ๋กœ ์ง์ ‘ ๋“ค์–ด๊ฐ€ ์žˆ์„ ๋•Œ ์ด๊ฒƒ์„ inline์ด๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

๋‹ค์Œ์€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ Hello world๋ฅผ ๊ฒฝ๊ณ ์ฐฝ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

<input type="button" onclick="alert('Hello world');" value="button" />

 

์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ๋Œ€์ƒ์„ ํ•„์š”๋กœํ•˜๋Š” ๊ฒฝ์šฐ this๋ฅผ ํ†ตํ•ด์„œ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. (this๋ฅผ ์ž๊ธฐ ์ž์‹ ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.)

์ด๋ฒคํŠธ๊ฐ€ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋Š” input ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ€๋ฅดํ‚ค๋ฏ€๋กœ ๊ฒฐ๊ตญ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ๋œ๋‹ค.

<!--์ž๊ธฐ ์ž์‹ ์„ ์ฐธ์กฐํ•˜๋Š” ๋ถˆํŽธํ•œ ๋ฐฉ๋ฒ•-->
<input type="button" id="target" onclick="alert('Hello world, '+document.getElementById('target').value);" value="button" />
<!--this๋ฅผ ํ†ตํ•ด์„œ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค-->
<input type="button" onclick="alert('Hello world, '+this.value);" value="button" />

this๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ ์ž์‹ ์˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์จ๋„ ์ƒ๊ด€์ด ์—†์ง€๋งŒ ์œ„์˜ ์ž๊ธฐ ์ž์‹ ์„ ์ฐธ์กฐํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ๊ฒฝ์šฐ target์„ ๊ฐ€๋ฅดํ‚ค๋Š” getElementById๋Š” ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

 

์ธ๋ผ์ธ  ๋ฐฉ์‹์€ ํƒœ๊ทธ์— ์ด๋ฒคํŠธ๊ฐ€ ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฒคํŠธ์˜ ์†Œ์žฌ๋ฅผ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ํŽธ๋ฆฌํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ •๋ณด์ธ HTML๊ณผ ์ œ์–ด์ธ JavaScript๊ฐ€ ํ˜ผ์žฌ๋œ ํ˜•ํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋žŒ์งํ•œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ• ์ˆ˜๋Š” ์—†๋‹ค. 

 


ํ”„๋กœํผํ‹ฐ ๋ฆฌ์Šค๋„ˆ

ํ”„๋กœํผํ‹ฐ ๋ฆฌ์Šค๋„ˆ ๋ฐฉ์‹์€ ์ด๋ฒคํŠธ ๋Œ€์ƒ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋กœ ์ด๋ฒคํŠธ๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

์ธ๋ผ์ธ ๋ฐฉ์‹์— ๋น„ํ•ด์„œ HTML๊ณผ JavaScript๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์„ ํ˜ธ๋˜๋Š” ๋ฐฉ์‹์ด์ง€๋งŒ ๋’ค์—์„œ ๋ฐฐ์šธ addEventListener ๋ฐฉ์‹์„ ์ถ”์ฒœํ•œ๋‹ค.

<input type="button" id="target" value="button" />
<script>
    var t = document.getElementById('target');
    t.onclick = function(){
        alert('Hello world');
    }
</script>

์œ„์™€ ๊ฐ™์ด ๊ฐ์ฒด๋กœ ์ •์˜ํ•˜์—ฌ ์ด๋ฒคํŠธ๋ฅผ ๋™์ž‘ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. 

 

 

์ด๋ฒคํŠธ ๊ฐ์ฒด

์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋œ ๋งฅ๋ฝ์˜ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋ฒคํŠธ ๊ฐ์ฒด๋Š” ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ๋‹ค. 

<body>
    <input type="button" id="target" value="button" />
<script>
    var t = document.getElementById('target');
    t.onclick = function(event){
        alert('Hello world, '+event.target.value)
    }
</script>

 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด์•ˆ์˜ ๋“ค์–ด์žˆ๋Š” ๊ฐ’์„ ํ™•์ธ ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰ ๋œ ์‹œ์ ์—์„œ์˜ ์ •๋ณด๋“ค์ด ๋‹ด๊ฒจ์žˆ๋‹ค.

<body>
    <input type="button" id="target" value="button" />
<script>
    var t = document.getElementById('target');
    t.onclick = function(event){
        console.dir(event);
    }
</script>

 

 

 

ie8 ์ดํ•˜ ๋ฒ„์ „์—์„œ๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ํ•ธ๋“ค๋Ÿฌ์˜ ์ธ์ž๊ฐ€ ์•„๋‹ˆ๋ผ ์ „์—ญ๊ฐ์ฒด์˜ event ํ”„๋กœํผํ‹ฐ๋กœ ์ œ๊ณตํ•œ๋‹ค. ๋˜ํ•œ target ํ”„๋กœํผํ‹ฐ๋„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๊ฐ€ ํฌ๋กœ์Šค ๋ธŒ๋ผ์šฐ์ง• ์ด์Šˆ ์ด๊ณ , ์œ„์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ์ง€์›์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— window๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

์•„๋ž˜๋Š” ์ด ๋ฌธ์ œ๋ฅผ ํ•ด์†Œํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋‹ค. 

<input type="button" id="target" value="button" />
<script>
    var t = document.getElementById('target');
    t.onclick = function(event){
        var event = event || window.event; // ์—ฌ๊ธฐ์„œ ||๋Š” ์•ž์˜ ๊ฐ’์ด ์—†๋‹ค๋ฉด ๋’ค์˜ ๊ฐ’์„ ์“ฐ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์ž„.
        var target = event.target || event.srcElement;//์—ฌ๊ธฐ์„œ๋„ srcElement๊ฐ€ ๊ทธ ์—ญํ• ์„ ํ•จ.
        alert('Hello world, '+target.value)
    }
</script>

์ฆ‰ ์•ž์— ์ฝ”๋“œ์˜ ์—†๋‹ค๋ฉด ๋’ค์— ์žˆ๋Š” ์ด์ „ ๋ฒ„์ „์—์„œ๋„ ์ง€์›๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.