๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/Vanilla JS

JS_๋ฌธ๋ฒ• (9)_์ด๋ฒคํŠธ

by KASSID 2022. 2. 24.

์›นํŽ˜์ด์ง€์— ์–ด๋– ํ•œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ

๊ทธ์— ๋Œ€ํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ์ƒํ˜ธ์ž‘์šฉ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

 

์˜ˆ์ „์—๋Š” ํƒœ๊ทธ์— ์ง์ ‘ ์†์„ฑ์œผ๋กœ ์—๋ฒคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹๋„ ์žˆ์—ˆ์ง€๋งŒ,

์ตœ๊ทผ์—๋Š” 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 ๋“ฑ)

 

 

 

๋Œ“๊ธ€