๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

javascript DOM ์š”์†Œ์˜ ์„ ํƒ ๋ฐ input, style, img ๋ณ€๊ฒฝ (2023.05.02) ๋ณธ๋ฌธ

full stack/JavaScript

javascript DOM ์š”์†Œ์˜ ์„ ํƒ ๋ฐ input, style, img ๋ณ€๊ฒฝ (2023.05.02)

Shinjio 2023. 5. 2. 14:57

๐ŸŽˆ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

input, select, textarea ๊ฐ™์ด ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” value ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•จ

 

<!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>input</title>
</head>
<body>
    <input type="text" id="inputTag">
    <button onclick="getInput()">ํด๋ฆญ</button>
    
    <script>
        // ๋ฒ„ํŠผ ํด๋ฆญ์‹œ, input ์•ˆ์— ์ž‘์„ฑ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ
        const getInput = ()=>{
            let inputVal = document.getElementById('inputTag');
            // input, select, textarea์™€ ๊ฐ™์ด ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›์•„์˜ค๋Š” ํƒœ๊ทธ -> value ์†์„ฑ์œผ๋กœ ์ ‘๊ทผ!!
            // innerText๋Š” ์‹œ์ž‘ํƒœ๊ทธ์™€ ๋ํƒœ๊ทธ ์‚ฌ์ด์˜ ์ฝ˜ํ…์ธ ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ
            alert(inputVal.value);
            console.log(inputVal.innerText);
        }
    </script>
</body>
</html>

 

innerTEXT๋กœ๋Š” ๊ฐ’์„ ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์—†์Œ

why? innerTEXT๋Š” ์‹œ์ž‘ํƒœ๊ทธ์™€ ๋ํƒœ๊ทธ ์‚ฌ์ด์˜ ์ฝ˜ํ…์ธ ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ

 

innerTEXT๋กœ ๋ฐ›์•„์˜จ ๊ฐ’ => ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅ X

 

๐Ÿ“– input ์‹ค์Šต

 

<!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>input Practice</title>
</head>
<body>
    <h1>์ž…๋ ฅํ•œ ๊ฐ’์„ h1ํƒœ๊ทธ๋กœ ์ถœ๋ ฅํ•ด๋ณด๊ธฐ</h1>
    <input type="text" id="inputTag">
    <button onclick="print()">h1ํƒœ๊ทธ ์ถœ๋ ฅ</button>

    <!-- ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด h1ํƒœ๊ทธ๋กœ ์ถœ๋ ฅ๋  ๊ณต๊ฐ„ -->
    <div id="divTag"></div>

    <script>
        // ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด h1 ํƒœ๊ทธ๋กœ ์ถœ๋ ฅ!!
        const print = ()=>{
            // 1. input ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ => ๋ณ€์ˆ˜์— ์ €์žฅ
            let inputVal = document.getElementById('inputTag');

            // 2. h1ํƒœ๊ทธ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ
            // 2-1. ์ถœ๋ ฅํ•  ๊ณต๊ฐ„ ๊ฐ€์ ธ์˜ค๊ธฐ
            // 2-2. h1ํƒœ๊ทธ ๊ธฐ๋Šฅ์„ ๋„ฃ์–ด์„œ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ
            document.getElementById('divTag').innerHTML += `<h1>${inputVal.value}</h1>`;

            // 3. input ํƒœ๊ทธ ์•ˆ ๋น„์›Œ์ฃผ๊ธฐ!
            inputVal.value = '';
        }
    </script>
</body>
</html>

 

Question. ๋ณ€์ˆ˜์— .value ์†์„ฑ ์ถ”๊ฐ€์‹œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ

<!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>input Practice</title>
</head>
<body>
    <h1>์ž…๋ ฅํ•œ ๊ฐ’์„ h1ํƒœ๊ทธ๋กœ ์ถœ๋ ฅํ•ด๋ณด๊ธฐ</h1>
    <input type="text" id="inputTag">
    <button onclick="print()">h1ํƒœ๊ทธ ์ถœ๋ ฅ</button>

    <!-- ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด h1ํƒœ๊ทธ๋กœ ์ถœ๋ ฅ๋  ๊ณต๊ฐ„ -->
    <div id="divTag"></div>

    <script>
        // ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด h1 ํƒœ๊ทธ๋กœ ์ถœ๋ ฅ!!
        const print = ()=>{
            // 1. input ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ => ๋ณ€์ˆ˜์— ์ €์žฅ
            let inputVal = document.getElementById('inputTag').value;

            // 2. h1ํƒœ๊ทธ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ
            // 2-1. ์ถœ๋ ฅํ•  ๊ณต๊ฐ„ ๊ฐ€์ ธ์˜ค๊ธฐ
            // 2-2. h1ํƒœ๊ทธ ๊ธฐ๋Šฅ์„ ๋„ฃ์–ด์„œ ๊ฐ’ ์ „๋‹ฌํ•˜๊ธฐ
            document.getElementById('divTag').innerHTML += `<h1>${inputVal}</h1>`;

            // 3. input ํƒœ๊ทธ ์•ˆ ๋น„์›Œ์ฃผ๊ธฐ!
            inputVal = '';
        }
    </script>
</body>
</html>
let inputVal = document.getElementById('inputTag').value;

์œ„์™€ ๊ฐ™์ด ๋ณ€์ˆ˜ ์ง€์ •์‹œ

inputVal = '';

์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์Œ

 

inputVal์— ํ• ๋‹น๋œ ๊ฐ’์€ ํ•ด๋‹น ์š”์†Œ์˜ ํ˜„์žฌ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ดํ›„์— inputVal ๋ณ€์ˆ˜์— ๋นˆ ๋ฌธ์ž์—ด์„ ํ• ๋‹นํ•ด๋„, ์›๋ž˜ ์š”์†Œ์˜ ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

document.getElementById('inputTag').value๋Š” 'inputTag'๋ผ๋Š” ID๋ฅผ ๊ฐ€์ง„ ์š”์†Œ์˜ value ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด value ํ”„๋กœํผํ‹ฐ๋Š” ํ•ด๋‹น ์š”์†Œ์˜ ํ˜„์žฌ ๊ฐ’(value)์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ inputVal ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ ๊ฐ’์€ ํ•ด๋‹น ์š”์†Œ์˜ ํ˜„์žฌ ๊ฐ’์„ ๋ณต์‚ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋งŒ์•ฝ 'inputTag' ์š”์†Œ๊ฐ€ <input type="text" id="inputTag" value="Hello World!">์™€ ๊ฐ™์ด ์„ ์–ธ๋˜์–ด ์žˆ๋‹ค๋ฉด, inputVal ๋ณ€์ˆ˜์—๋Š” "Hello World!"๋ผ๋Š” ๋ฌธ์ž์—ด์ด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.
์ดํ›„์— inputVal์— ๋นˆ ๋ฌธ์ž์—ด์„ ํ• ๋‹นํ•˜๋”๋ผ๋„, ์ด๋Š” inputVal ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ ๋ฌธ์ž์—ด ๊ฐ’๋งŒ์„ ๋ณ€๊ฒฝํ•  ๋ฟ, ์‹ค์ œ HTML ๋ฌธ์„œ ๋‚ด์˜ 'inputTag' ์š”์†Œ์˜ ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ, inputVal ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ ํ˜„์žฌ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋”๋ผ๋„, ์ด๋Š” ๋‹จ์ง€ ๋ณ€์ˆ˜ ๋‚ด์˜ ๊ฐ’๋งŒ ๋ณ€๊ฒฝํ•  ๋ฟ, HTML ๋ฌธ์„œ ๋‚ด์˜ ์š”์†Œ์˜ ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ์ ์„ ์œ ์˜ํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์š”์†Œ ์ ‘๊ทผ ์†์„ฑ

 

๐ŸŽˆ Element ์Šคํƒ€์ผ(style) ๋ณ€๊ฒฝ

 

 

๐Ÿ“– ์š”์†Œ ์กฐํšŒ 

 

 

         // span ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ
            // (1) getElement~
            let getSpan1 = document.getElementById('spanTag');
            console.log(getSpan1);
            let getSpan2 = document.getElementsByTagName('span');
            // getElements => HTMLCollection์œผ๋กœ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ => index ๋ฒˆํ˜ธ๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ
            console.log(getSpan2[0]);
            // (2) quearySelector~
            // (2-1) ํƒœ๊ทธ ์„ ํƒ์ž
            let selectSpan1 = document.querySelector('span');
            console.log(selectSpan1);
            // (2-2) ์•„์ด๋”” ์„ ํƒ์ž
            let selectSpan2 = document.querySelector('#spanTag');
            console.log(selectSpan2);

        const clickFunc = ()=>{

            // querySelector : CSS ์„ ํƒ์ž๋กœ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰
            // querySelectorAll : CSS ์„ ํƒ์ž๋กœ ๋ชจ๋“  ์š”์†Œ ๊ฒ€์ƒ‰

            // DOM ์Šคํƒ€์ผ ์ œ์–ด
            // ์š”์†Œ.style.์†์„ฑ = ๊ฐ’

                // ๊ธ€์žํฌ๊ธฐ 25px
                getSpan1.style.fontSize = '25px';
                // ๊ธ€์ž์ƒ‰์ƒ ํ† ๋งˆํ† 
                getSpan1.style.color = 'tomato';
                // ๊ธ€์ž๊ตต๊ธฐ 900
                getSpan1.style.fontWeight = '900';
        }
// ์ด๋™ํ•˜๊ธฐ ๋ฒ„ํŠผ ํด๋ฆญ์‹œ
// ๋ฐ•์Šค๋“ค์ด ๊ณ„๋‹จ ํ˜•ํƒœ๋กœ ์ด๋™

// ๋‘ฅ๊ธ€๊ฒŒ ๋ฒ„ํŠผ ํด๋ฆญ์‹œ
// ์˜ค๋ฅธ์ชฝ ์œ„, ์™ผ์ชฝ ์•„๋ž˜ ๋ชจ์„œ๋ฆฌ ๋‘ฅ๊ธ€๊ฒŒ

// ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์š”์†Œ ์ „์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ → querySelectAll
let box = document.querySelectorAll('.box');
// ์œ ์‚ฌ ๋ฐฐ์—ด๋กœ ๋„˜์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋กœ ์ ‘๊ทผํ•ด์ค˜์•ผ ํ•จ
let blueBox = document.querySelector('#blue');
let greenBox = document.querySelector('#green');
let grayBox = document.querySelector('#gray');

const moveFunc = ()=>{
    blueBox.style.marginLeft = '100px';
    greenBox.style.marginLeft = '200px';
    grayBox.style.marginLeft = '300px';
}

const roundFunc = ()=>{
    // ๋ฐฉ๋ฒ•1) for of ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ
    for(let i of box){
        i.style.borderTopRightRadius = '50%';
        i.style.borderBottomLeftRadius = '50%';        
    }

    // forEach ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ
    // querySelectAll => return ํƒ€์ž…์ด nodeList (๋ฆฌ์ŠคํŠธ์ด๋ฏ€๋กœ forEach ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
    // getElementsByTagName => return ํƒ€์ž…์ด HTMLCollection (ํ•จ์ˆ˜ ์‚ฌ์šฉ ๋ถˆ๊ฐ€)
    box.forEach(element => {
        element.style.borderTopRightRadius = '50%';
        element.style.borderBottomLeftRadius = '50%';
    })
}

 

โš™ NodeList์™€ HTMLCollection์˜ ์ฐจ์ด

 

HTMLCollection๊ณผ NodeList๋Š” ๋‘˜ ๋‹ค HTML ๋ฌธ์„œ ๋‚ด์˜ ์š”์†Œ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด ๋‘˜์€ ๋ช‡ ๊ฐ€์ง€ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒซ์งธ, ๋…ธ๋“œ ํƒ€์ž…์˜ ์ฐจ์ด์ ์ž…๋‹ˆ๋‹ค.
HTMLCollection์€ HTML ๋ฌธ์„œ ๋‚ด์—์„œ ์š”์†Œ(element)๋“ค๋งŒ์„, NodeList๋Š” HTML ๋ฌธ์„œ ๋‚ด์—์„œ ์š”์†Œ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ…์ŠคํŠธ ๋…ธ๋“œ(text node), ์ฃผ์„ ๋…ธ๋“œ(comment node), ์†์„ฑ(attribute) ๋“ฑ ๋ชจ๋“  ๋…ธ๋“œ ํƒ€์ž…(node type)์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

๋‘˜์งธ, ์—…๋ฐ์ดํŠธ ๋ฐฉ์‹์˜ ์ฐจ์ด์ ์ž…๋‹ˆ๋‹ค.
HTMLCollection์€ ๋™์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ๋˜๋ฉฐ, DOM์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ฉ๋‹ˆ๋‹ค. NodeList๋Š” ์ •์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ๋˜๋ฉฐ, NodeList ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ์˜ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.

์…‹์งธ, ์†๋„์˜ ์ฐจ์ด์ ์ž…๋‹ˆ๋‹ค.
NodeList๋Š” ์ง์ ‘ ๋ฐ˜๋ณต(iteration)์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ, ์†๋„๊ฐ€ HTMLCollection๋ณด๋‹ค ๋น ๋ฆ…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, HTMLCollection์€ ๋ผ์ด๋ธŒ(live) ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋ณ€๊ฒฝ๋œ DOM์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ˜์˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ƒํ™ฉ์— ๋”ฐ๋ผ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋„ท์งธ, ํŠน์„ฑ์˜ ์ฐจ์ด์ ์ž…๋‹ˆ๋‹ค.
HTMLCollection์€ item() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. NodeList๋Š” ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, NodeList๋Š” forEach() ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์›ํ•˜์—ฌ ๋ฐ˜๋ณต(iteration)์„ ๋”์šฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์š”์•ฝํ•˜์ž๋ฉด, HTMLCollection์€ ์—…๋ฐ์ดํŠธ๊ฐ€ ๋ผ์ด๋ธŒ(live)๋˜๋ฉฐ ์š”์†Œ๋งŒ์„ ํฌํ•จํ•˜๊ณ , NodeList๋Š” ์—…๋ฐ์ดํŠธ๊ฐ€ ์ •์ (static)์ด๋ฉฐ ๋ชจ๋“  ๋…ธ๋“œ ํƒ€์ž…์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

๐ŸŽˆ ์ด๋ฏธ์ง€ ์†์„ฑ ๋ณ€๊ฒฝ

<!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>img Practice</title>
</head>
<body>
    <img src="https://i.ytimg.com/vi/chdsfO4Db1Q/maxresdefault.jpg" alt="" id="imgId" class="imgClass">
    <br>
    <button onclick="imgChange()">์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ</button>

    <script>
        // ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ์ด๋ฏธ์ง€ ๋ณ€๊ฒฝ!

        // 1. ์ด๋ฏธ์ง€ ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ
        let imgVal = document.getElementById('imgId');
        console.log(imgVal);
        let imgVal2 = document.getElementsByClassName('imgClass')[0];
        console.log(imgVal2);
        let imgVal3 = document.querySelector('img');
        console.log(imgVal3);
        let imgVal4 = document.querySelector('#imgId');
        console.log(imgVal4);
        let imgVal5 = document.querySelector('.imgClass');
        console.log(imgVal5);
        let imgVal6 = document.querySelectorAll('img')[0];
        console.log(imgVal6);
        let imgVal7 = document.querySelectorAll('.imgClass')[0];
        console.log(imgVal7);

        // ์ด๋ฏธ์ง€ ์ฃผ์†Œ ๋ณ€์ˆ˜์— ๋‹ด๊ธฐ 
        let imgSrc1 = 'https://i.ytimg.com/vi/2lMngDTyFgM/maxresdefault.jpg';
        let imgSrc2 = 'https://i.ytimg.com/vi/chdsfO4Db1Q/maxresdefault.jpg';

        let sw = true;
        const imgChange = ()=>{

            // ๋ฐฉ๋ฒ• 1) booleanํƒ€์ž… sw ํ™œ์šฉ
            // if(sw){
            //     imgVal.src = 'https://i.ytimg.com/vi/2lMngDTyFgM/maxresdefault.jpg';
            //     sw = false;
            // }else{
            //     imgVal.src = 'https://i.ytimg.com/vi/chdsfO4Db1Q/maxresdefault.jpg';
            //     sw = true;
            // }

            // ๋ฐฉ๋ฒ• 2) ์ด๋ฏธ์ง€ ์ฃผ์†Œ ํ™œ์šฉ
            // 1. img src๊ฐ€ 1๋ฒˆ ์ฃผ์†Œ๋ผ๋ฉด
            // 2. src๋ฅผ 2๋ฒˆ ์ฃผ์†Œ๋กœ
            // 3. ์•„๋‹ˆ๋ผ๋ฉด 1๋ฒˆ ์ฃผ์†Œ๋กœ.
            if(imgVal.src == imgSrc1){
                imgVal.src = imgSrc2
            }else{
                imgVal.src = imgSrc1;
            }
        }
    </script>
</body>
</html>