์ƒˆ์†Œ์‹

Languages/Java Script

๐Ÿ‘‰๐Ÿป[Js] addEventListener() | ์ด๋ฒคํŠธ ์ „ํŒŒ | ๋ฒ„๋ธ”๋ง๊ณผ ์บก์ฒ˜๋ง

  • -
๋ฐ˜์‘ํ˜•

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

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "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


addEventListener()

addEventListener์€ ์ด๋ฒคํŠธ๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๊ฐ€์žฅ ๊ถŒ์žฅ๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ด ๋ฐฉ์‹์„ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.

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

์œ„ ๋ฐฉ์‹์€ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ์ด๋ฒคํŠธ ๋ฐฉ์‹์„ ์„ค๋ช…ํ•œ๋‹ค. ์ฆ‰ t๋ณ€์ˆ˜๋ฅผ ํด๋ฆญํ•  ์‹œ ๋’ค์— ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

๋˜ํ•œ event.target.value๋Š” button์„ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰ ์ด์ „ ๋ฐฉ์‹๋“ค์˜ ์ข‹์€ ์ ๋“ค์„ ๋‹ค ๊ฐ€์ ธ๋‹ค ๋†“์€ ๊ฒƒ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ด ๋ฐฉ์‹์€ ie8์ดํ•˜์—์„œ๋Š” ํ˜ธํ™˜๋˜์ง€ ์•Š๋Š”๋‹ค. ie์—์„œ๋Š” attachEvent ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

var t = document.getElementById('target');
if(t.addEventListener){
    t.addEventListener('click', function(event){
        alert('Hello world, '+event.target.value);
    }); 
} else if(t.attachEvent){
    t.attachEvent('onclick', function(event){
        alert('Hello world, '+event.target.value);
    })
}

์œ„ ์ฝ”๋“œ๋Š” addEventListener๋ฐฉ์‹์ด ํ˜ธํ™˜์ด ๋˜์ง€ ์•Š์•„ ์‚ฌ์šฉ์ด ์•ˆ๋œ๋‹ค๋ฉด, ์ด๋•Œ attachEvent๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

๊ฑฐ์˜ ๋ฐฉ์‹์ด ๋™์ผํ•˜์ง€๋งŒ, ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ์˜ค๋Š” ๊ฐ’์— ๋ฌด์กฐ๊ฑด on์ด ๋ถ™์–ด์•ผ ํ•œ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ์ตœ๊ทผ์—๋Š” ์œ„์™€ ๊ฐ™์€ ํฌ๋กœ์Šค ๋ธŒ๋ผ์šฐ์ง• ์ด์Šˆ์˜ ๋ฌธ์ œ๋ฅผ ๋Œ€๋ถ€๋ถ„์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

์ด ๋ฐฉ์‹์˜ ์ค‘์š”ํ•œ ์žฅ์ ์€ ํ•˜๋‚˜์˜ ์ด๋ฒคํŠธ ๋Œ€์ƒ์— ๋ณต์ˆ˜์˜ ๋™์ผ ์ด๋ฒคํŠธ ํƒ€์ž… ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค.  ์ด ๋ฐฉ์‹์€ ํ”„๋กœํผํ‹ฐ์™€ ์ธ๋ผ์ธ ๋ฐฉ์‹๊ณผ ์ฐจ์ด๋ฅผ ๋ณด์ธ๋‹ค.

<input type="button" id="target" value="button" />
<script>
    var t = document.getElementById('target');
    t.addEventListener('click', function(event){
        alert(1);
    });
    t.addEventListener('click', function(event){
        alert(2);
    });
</script>

๊ธฐ์กด์˜ ํ”„๋กœํผํ‹ฐ ๋ฐฉ์‹์—์„œ๋Š”  ๋‹จ ํ•˜๋‚˜์˜ ์ด๋ฒคํŠธ๋งŒ์„ ์„ค์ • ํ•  ์ˆ˜ ์žˆ์–ด์„œ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์‚ฌ๋ผ์ง€๋ฏ€๋กœ, ์ œ์ผ ๋งˆ์ง€๋ง‰์— ์‚ฌ์šฉํ•œ ์ด๋ฒคํŠธ๋งŒ ๋™์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๊ด‘๋Œ€ํ•œ ์ฝ”๋“œ์˜ ๊ฒฝ์šฐ add์ด๋ฒคํŠธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

 

ํ•˜๋‚˜์˜ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ด๋ฒคํŠธ ํƒ€๊ฒŸ์— ์„ค์น˜ํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ณต์ˆ˜์˜ ์—˜๋ฆฌ๋จผํŠธ์— ํ•˜๋‚˜์˜ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋“ฑ๋กํ•ด์„œ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

<input type="button" id="target1" value="button1" />
<input type="button" id="target2" value="button2" />
<script>
    var t1 = document.getElementById('target1');
    var t2 = document.getElementById('target2');
    function btn_listener(event){
        switch(event.target.id){
            case 'target1':
                alert(1);
                break;
            case 'target2':
                alert(2);
                break;
        }
    }
    t1.addEventListener('click', btn_listener);
    t2.addEventListener('click', btn_listener);
</script>

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋กœ ์‚ฌ์šฉํ•  ํ•จ์ˆ˜ : btn_listener() // target1์ด๋‚˜ 2 ๋‘˜๋‹ค ์œ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

event.target.id๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ๊ฐ์˜ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ๊ฐ€์ง„ id๊ฐ’์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ตฌ๋ถ„ํ•˜์—ฌ ๊ฐ ์ด๋ฒคํŠธ๋ฅผ ๋™์ž‘์‹œ์ผœ์ค€๋‹ค.


์ด๋ฒคํŠธ ์ „ํŒŒ(๋ฒ„๋ธ”๋ง๊ณผ ์บก์ฒ˜๋ง)

์ด๋ฒˆ ๊ฐ•์˜์˜ ๋‚ด์šฉ์€ ๋งค์šฐ ๋‚œ์ด๋„๊ฐ€ ๋†’๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ์‹ค์Šต์„ ๋™๋ฐ˜ํ•˜์—ฌ ๊ฐ•์˜๋ฅผ ์ˆ™์ง€ํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค.

JavaScript - ์ด๋ฒคํŠธ์ „ํŒŒ (1/3) : ์†Œ๊ฐœ

HTML ํƒœ๊ทธ๋Š” ์ค‘์ฒฉ๋˜์–ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŠน์ •ํ•œ ํƒœ๊ทธ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋Š” ์ค‘์ฒฉ๋˜์–ด ์žˆ๋Š” ํƒœ๊ทธ๋“ค ๋ชจ๋‘๊ฐ€ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ์ค‘์ฒฉ๋œ ํƒœ๊ทธ๋“ค์— ์ด๋ฒคํŠธ๊ฐ€ ๋“ฑ๋ก ๋˜์–ด ์žˆ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌ ๋ ๊นŒ? (์‹คํ–‰)

<html>
    <head>
        <style>
            html{border:5px solid red;padding:30px;}
            body{border:5px solid green;padding:30px;}
            fieldset{border:5px solid blue;padding:30px;}
            input{border:5px solid black;padding:30px;}
        </style>
    </head>
    <body>
        <fieldset>
            <legend>event propagation</legend>
            <input type="button" id="target" value="target">          
        </fieldset>
        <script>
        function handler(event){
            var phases = ['capturing', 'target', 'bubbling']
            console.log(event.target.nodeName, this.nodeName, phases[event.eventPhase-1]);//์ฒซ ๋ฒˆ์งธ ์ธ์ž ์ฆ‰ event.target.nodeName์€ ๊ฐ€์žฅ ๋‚ด๋ถ€์˜ ์žˆ๋Š” ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋ฆฌํ„ด
        }
        document.getElementById('target').addEventListener('click', handler, true);
        document.querySelector('fieldset').addEventListener('click', handler, true);
        document.querySelector('body').addEventListener('click', handler, true);
        document.querySelector('html').addEventListener('click', handler, true);
        </script>
    </body>
</html>

 

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๊ฐ€ ์•ˆ์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” ๊ด€๊ณ„์—์„œ ์œ„์—์„œ๋ถ€ํ„ฐ ์ฆ‰, ๊ฐ€์žฅ ํฐ ํƒœ๊ทธ๋ถ€ํ„ฐ ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์„ "์บก์ฒ˜๋ง(capturing)" ์ด๋ผ๊ณ  ํ•œ๋‹ค.

๋ฐ˜๋Œ€๋กœ ์ œ์ผ ๊นŠ์ˆ™ํ•œ ๋‚ด๋ถ€์—์„œ๋ถ€ํ„ฐ ๋™์ž‘ํ•˜์—ฌ ์œ„๋กœ ํ˜ธ์ถœ๋˜๋Š” ๊ณผ์ •์„ "๋ฒ„๋ธ”๋ง(bubbling)"์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œ์ผœ๋ณด๋ฉด ์œ„์™€ ๊ฐ™์ด ๊ตฌ์—ญ์ด ๋‚˜๋‰˜๊ฒŒ ๋œ๋‹ค.

 

์‹คํ–‰๊ฒฐ๊ณผ

INPUT HTML capturing
INPUT BODY capturing
INPUT FIELDSET capturing
INPUT INPUT target

์ด๋ฒคํŠธ๊ฐ€ ๋ถ€๋ชจ์—์„œ๋ถ€ํ„ฐ ๋ฐœ์ƒํ•ด์„œ ์ž์‹์œผ๋กœ ์ „ํŒŒ๋˜๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์„ capturing์ด๋ผ๊ณ  ํ•œ๋‹ค. ie ๋‚ฎ์€ ๋ฒ„์ „์—์„œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์ด ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š๋Š”๋‹ค.

 

์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•ด๋ณด์ž. (์‹คํ–‰)

document.getElementById('target').addEventListener('click', handler, false);
document.querySelector('fieldset').addEventListener('click', handler, false);
document.querySelector('body').addEventListener('click', handler, false);
document.querySelector('html').addEventListener('click', handler, false);

์ฐจ์ด์ ์€ addEventListener์˜ 3๋ฒˆ์งธ ์ธ์ž๊ฐ€ false๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค!

 

 

์‹คํ–‰๊ฒฐ๊ณผ

INPUT INPUT target
INPUT FIELDSET bubbling
INPUT BODY bubbling
INPUT HTML bubbling

์ด๋ฒˆ์—๋Š” ์ˆœ์„œ๊ฐ€ ๋ฐ˜๋Œ€๋กœ ๋˜์—ˆ๋‹ค. ์ž์‹๋ถ€ํ„ฐ ๋ถ€๋ชจ๋กœ ์ด๋ฒคํŠธ๊ฐ€ ์ „ํŒŒ๋˜๋Š” ๊ฒƒ์„ ๋ฒ„๋ธ”๋ง(bubbling)์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

์ฆ‰, ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ํ†ตํ•ด addEventListner์˜ ์„ธ๋ฒˆ์งธ ์ธ์ž๊ฐ€ ์–ด๋– ํ•œ ์—ญํ• ์„ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.(:use capturing์„ ์˜๋ฏธํ•จ)

๋ฒ„๋ธ”๋ง๊ณผ ์บก์ฒ˜๋ง์€ ์œ„ ํ•จ์ˆ˜์˜ ์„ธ๋ฒˆ์งธ ์ธ์ž ๊ฐ’์„ true๋กœ ์ฃผ๋Š๋ƒ false๋กœ ์ฃผ๋Š๋ƒ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๊ฒŒ ๋œ๋‹ค.

 

์ด๋ฒคํŠธ ์ „ํŒŒ๋ฅผ ์ค‘๊ฐ„์— ๊ฐ€๋กœ๋ง‰์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์‹œ๋Š” body๊นŒ์ง€๋งŒ ์ด๋ฒคํŠธ๊ฐ€ ์ „ํŒŒ๋˜๋„๋ก ์ค‘๊ฐ„์— ๋ง‰๋Š” ์˜ˆ์‹œ๋ฅผ ์ฝ”๋“œ๋กœ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ด๋‹ค.

 

์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด๋ณด์ž. (์‹คํ–‰)

function handler(event){
    var phases = ['capturing', 'target', 'bubbling']
    console.log(event.target.nodeName, this.nodeName, phases[event.eventPhase-1]);
}
function stophandler(event){
    var phases = ['capturing', 'target', 'bubbling']
    console.log(event.target.nodeName, this.nodeName, phases[event.eventPhase-1]);
    event.stopPropagation();//event๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ ๋ฉ”์†Œ๋“œ stop...() ํ˜ธ์ถœ
}
document.getElementById('target').addEventListener('click', handler, false);
document.querySelector('fieldset').addEventListener('click', handler, false);
document.querySelector('body').addEventListener('click', stophandler, false);
document.querySelector('html').addEventListener('click', handler, false);

 

์‹คํ–‰๊ฒฐ๊ณผ

INPUT INPUT target
INPUT FIELDSET bubbling
INPUT BODY bubbling

์ฐธ๊ณ ๋กœ ie9 ์ด์ „์˜ ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์ด๋ฒคํŠธ ์ „ํŒŒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ event.cancelBubble ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

๋ฐ˜์‘ํ˜•
Contents

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

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