λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
🌌 | WEB DEV/Vanilla JS

JS_문법 (9)_이벀트

by KASSID 2022. 2. 24.

λͺ©μ°¨

    728x90

    μ›ΉνŽ˜μ΄μ§€μ— μ–΄λ– ν•œ μ΄λ²€νŠΈκ°€ λ°œμƒν–ˆμ„ λ•Œ

    그에 λŒ€ν•΄ μ‚¬μš©μžκ°€ μƒν˜Έμž‘μš©μ„ ν•  수 있게 ν•˜λŠ” 것에 λŒ€ν•΄μ„œ μ•Œμ•„λ³΄μž.

     

    μ˜ˆμ „μ—λŠ” νƒœκ·Έμ— 직접 μ†μ„±μœΌλ‘œ μ—λ²€νŠΈλ₯Ό μΆ”κ°€ν•˜λŠ” 방식도 μžˆμ—ˆμ§€λ§Œ,

    μ΅œκ·Όμ—λŠ” JSλ₯Ό HTMLκ³Ό λΆ„λ¦¬ν•˜κΈ°μœ„ν•΄μ„œ

    addEventListener()λ₯Ό μ‚¬μš©ν•˜λŠ” 방식을 μ„ ν˜Έν•œλ‹€.

     

    이벀트 등둝

    λŒ€μƒ μš”μ†Œ.addEvenListener(이벀트λͺ…, λ¦¬μŠ€λ„ˆν•¨μˆ˜(μ½œλ°±ν•¨μˆ˜))

    이벀트λͺ…은

    click, intput, focus, mousehover 등을 μ΄μ•ΌκΈ°ν•œλ‹€.

     

    λ¦¬μŠ€λ„ˆν•¨μˆ˜λŠ” λ§κ·ΈλŒ€λ‘œ 'ν•¨μˆ˜'λ₯Ό λ„£μ–΄μ£Όλ©΄ λœλ‹€.

    λ”°λΌμ„œ 인수 μžλ¦¬μ— ν•¨μˆ˜λ₯Ό μ •μ˜ν•΄μ£Όκ±°λ‚˜ μ •μ˜ν•œ ν•¨μˆ˜μ˜ 이름을 λ„£μ–΄μ€€λ‹€.

    (ν•¨μˆ˜μ˜ 이름이 μ•„λ‹Œ "ν•¨μˆ˜λͺ…()"의 ν˜•νƒœλ₯Ό λ„£μ–΄μ£Όλ©΄ λ°˜ν™˜κ°’μ΄ λ“€μ–΄κ°€λ―€λ‘œ μ •μƒμž‘λ™X)

     

    이벀트 μ‚­μ œ

    λŒ€μƒ μš”μ†Œ.removeEvenListener(이벀트λͺ…, 이벀트 λ°œμƒμ‹œ μ‹€ν–‰ ν•¨μˆ˜)

    등둝할 λ•Œ μ μš©ν•œ 인수λ₯Ό κ·ΈλŒ€λ‘œ 써주어야 ν•œλ‹€.

     

    μ˜ˆμ‹œ 1

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .box1 {
                width: 100px;
                height: 100px;
                border-radius: 5px;
                line-height: 100px;
                background: blueviolet;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class="box1">Hello!</div>
        <script>
            const box1 = document.querySelector(".box1")	//μ„ νƒμžλ₯Ό μ΄μš©ν•΄ λ³€μˆ˜μ— λ‹΄κΈ°
            box1.addEventListener("click", () => {	//ν•΄λ‹Ή 블둝을 ν΄λ¦­ν–ˆμ„ λ•Œ
              alert("Hello!");	//μ•Œλ¦Όμ°½ λ„μš°κΈ°
            });
        </script>
    </body>
    </html>

     

    μ˜ˆμ‹œ 2

    addEventListener ν™œμš©

    λ…Έλž€μƒ‰ λ²„νŠΌμ€ 생성, 주황색 λ²„νŠΌμ€ μ‚­μ œλ₯Ό ν•˜λŠ” μ½”λ“œμ΄λ‹€.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .container {
                display: flex;
                flex-direction: row;
            }
            .button1 {
                width: 50px;
                height: 20px;
                background: gold;
            }
            .button2 {
                width: 50px;
                height: 20px;
                background: orangered;
            }
            .box1 {
                width: 100px;
                height: 100px;
                border-radius: 5px;
                line-height: 100px;
                background: blueviolet;
                text-align: center;
                margin: 2px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="button1"></div>
            <div class="button2"></div>
        </div>
        <script>
            const button1 = document.querySelector(".button1")
            const button2 = document.querySelector(".button2")
            let newBox = document.createElement("div")
            button1.addEventListener("click", () => {
                newBox.setAttribute("class", "box1")
                document.body.appendChild(newBox)
            });
            button2.addEventListener("click", () => {
                document.body.removeChild(newBox)
            });
        </script>
    </body>
    </html>

     

    μ˜ˆμ‹œ 3

    removeEventListener ν™œμš©

    보라색 블둝을 λˆŒλ €μ„ λ•Œ

    μ‚­μ œλ²„νŠΌμ˜ 이벀트λ₯Ό μ‚­μ œν•˜λŠ” μ½”λ“œμ΄λ‹€.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            div:hover {
                cursor: pointer;
            }
            .container {
                display: flex;
                flex-direction: row;
            }
            .button1 {
                width: 50px;
                height: 20px;
                background: gold;
            }
            .button2 {
                width: 50px;
                height: 20px;
                background: orangered;
            }
            .box1 {
                width: 100px;
                height: 100px;
                border-radius: 5px;
                line-height: 100px;
                background: blueviolet;
                text-align: center;
                margin: 2px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="button1"></div>
            <div class="button2"></div>
        </div>
        <script>
            const createBox = () => {
                newBox.setAttribute("class", "box1")
                document.body.appendChild(newBox)
            }
            const removeBox = () => {
                document.body.removeChild(newBox)
            }
    
            const button1 = document.querySelector(".button1")
            const button2 = document.querySelector(".button2")
            let newBox = document.createElement("div")
            button1.addEventListener("click", createBox);
            button2.addEventListener("click", removeBox);
    
            newBox.addEventListener("click", () =>{
                newBox.innerText = "Fixed!"
                button2.removeEventListener("click", removeBox);
            })
            
        </script>
    </body>
    </html>

    μ˜ˆμ‹œ4

    μ‚¬μš©μžλ‘œλΆ€ν„°μ˜ μž…λ ₯κ°’ ν™œμš©

    <input type="text" name="" id="word-input">
    <button>μž…λ ₯</button>
    <script>
        document.querySelector('input').addEventListener('input', (event) => {
            console.log(event.target.value);
        });
        document.querySelector('button').addEventListener('click', () => {
            console.log("λ²„νŠΌ 클릭!");
        });
    </script>

    eventλ₯Ό λ¦¬μŠ€λ„ˆν•¨μˆ˜μ— 인수둜 μ‚½μž…ν•˜κ³ 

    event.target.valueλ₯Ό μ΄μš©ν•˜μ—¬ μž…λ ₯값을 κ°€μ Έμ˜¬ 수 μžˆλ‹€.

    input μ΄λ²€νŠΈλŠ” μž…λ ₯창에 λ³€ν™”κ°€ μžˆμ„ λ•Œλ§ˆλ‹€ μΌμ–΄λ‚˜λ―€λ‘œ

    λ²„νŠΌ 클릭 μ΄λ²€νŠΈκ°€ μΌμ–΄λ‚œ μ‹œμ μ˜ 직전 μƒνƒœλ₯Ό μ΄μš©ν•˜λ©΄

    μœ μ €κ°€ μ˜λ„ν•œ 값을 ν™œμš©ν•  수 μžˆλ‹€.

     

    β€».vlaue / .textContent

    μš”μ†Œμ˜ μ’…λ₯˜μ— λ”°λΌμ„œ λ‚΄λΆ€μ˜ 값을 κ°€μ Έμ˜¬ λ•Œ .value 와 .textContent둜 λ‹€λ₯Έ κ²½μš°κ°€ μžˆλ‹€.

    이λ₯Ό μΌλ°˜ν™”μ‹œν‚€λ©΄ 'μž…λ ₯을 ν•˜λŠ” 경우'μ—λŠ” value이닀.

    ( .value : input, select, textarea λ“±)

    ( .textContent : button, div, span λ“±)

     

     

     

    λŒ“κΈ€