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

JiYoung Dev ๐Ÿ–ฅ

javascript DOM (2023.05.01) ๋ณธ๋ฌธ

full stack/JavaScript

javascript DOM (2023.05.01)

Shinjio 2023. 5. 1. 23:13

๐ŸŽˆ DOM (Document Object Model)

HTML ๋ฌธ์„œ์˜ ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ์ฒด๋กœ ๋ชจ๋ธ๋ง

๋ฌธ์„œ ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ •์˜ํ•˜๊ณ , ๊ฐ๊ฐ์˜ ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณต

 

๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(DOM)์€ HTML, XML ๋ฌธ์„œ์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ธํ„ฐํŽ˜์ด์Šค์ž„. DOM์€ ๋ฌธ์„œ์˜ ๊ตฌ์กฐํ™”๋œ ํ‘œํ˜„์„ ์ œ๊ณตํ•˜๋ฉฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๊ฐ€ DOM ๊ตฌ์กฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜์—ฌ ๊ทธ๋“ค์ด ๋ฌธ์„œ ๊ตฌ์กฐ, ์Šคํƒ€์ผ, ๋‚ด์šฉ ๋“ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์›€. 

 

 

์›นํŽ˜์ด์ง€๋Š” ์ผ์ข…์˜ ๋ฌธ์„œ(document)์ž„. ์ด ๋ฌธ์„œ๋Š” ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ†ตํ•ด ๊ทธ ๋‚ด์šฉ์ด ํ•ด์„๋˜์–ด ์›น ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๊ฑฐ๋‚˜ HTML ์†Œ์Šค ์ž์ฒด๋กœ ๋‚˜ํƒ€๋‚จ. 

 

๐Ÿ“– HTML ๋ฌธ์„œ์˜ ํŠธ๋ฆฌ๊ตฌ์กฐ

 

 

Document ๊ฐ์ฒด๋Š” ์›น ํŽ˜์ด์ง€ ๊ทธ ์ž์ฒด๋ฅผ ์˜๋ฏธํ•จ. ์›น ํŽ˜์ด์ง€์— ์กด์žฌํ•˜๋Š” HTML ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•  ๋•Œ๋Š” ๋ฐ˜๋“œ์‹œ Document ๊ฐ์ฒด๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์•ผ ํ•จ

 

HTMLElement
๋ชจ๋“  ์ข…๋ฅ˜์˜ HTML ์š”์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ธํ„ฐํŽ˜์ด์Šค
getElement ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์›ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์กฐํšŒ
์กฐํšŒ๋œ ๊ฐ์ฒด๋“ค์„ ๋Œ€์ƒ์œผ๋กœ ๊ตฌ์ฒด์ ์ธ ์ž‘์—… ์ฒ˜๋ฆฌ

 

โš™๏ธ Document๋กœ ์ ‘๊ทผํ•ด์„œ HTML ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ

<!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>Dom Start</title>
</head>
<body>
    <p id="pTag">DOM ์‹œ์ž‘ํ•˜๊ธฐ</p>


    <script>
        // ๋ฌธ์„œ์— → document
        // ์ ‘๊ทผํ•ด์„œ → .
        // ๊ฐ€์ ธ์˜ฌ๊ฑฐ์•ผ → get
        // ์š”์†Œ๋ฅผ → Element
        // ์•„์ด๋””๋กœ → ById

        let pElement = document.getElementById('pTag');
        console.log(pElement);
    </script>
</body>
</html>

 

๊ฐ์ฒด ๋ฐ˜ํ™˜ ๋ฉ”์„œ๋“œ

id๋Š” html ๋‚ด ๋‹จ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ  → element

๋‚˜๋จธ์ง€๋Š” → elements

 

HTMLCollection ๊ฐ์ฒด

์œ ์‚ฌ ๋ฐฐ์—ด์˜ ํ˜•ํƒœ

๋ฐฐ์—ด์˜ ๊ธธ์ด์™€ ์ธ๋ฑ์Šค๋Š” ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋‚˜ ๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ(push, pop)์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ

 

 

 

โš™๏ธ DOM ์‚ฌ์šฉํ•˜๊ธฐ ์‹ค์Šต

<!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>DOM Property</title>
</head>
<body>
    <div id="container">
        <p>DOM ์‚ฌ์šฉํ•˜๊ธฐ</p>
    </div>
    <button id="textBtn">TEXT</button>
    <!-- 
        ์ธ๋ผ์ธ ์Šคํฌ๋ฆฝํŠธ๋กœ ํด๋ฆญ ์ด๋ฒคํŠธ ์ฃผ๊ธฐ
        onclick = "ํ•จ์ˆ˜์ด๋ฆ„()"
    -->
    <button onclick="htmlFunc()">HTML</button>

    <script>
        // 1. div ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ
        let domVal = document.getElementById('container');
        console.log('์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ :', domVal);

        // 2. TEXT ๋ฒ„ํŠผ ํด๋ฆญ์‹œ, ์š”์†Œ์—์„œ ๋‚ด์šฉ๋งŒ ์ถœ๋ ฅ๋˜๋„๋ก
        // addEventListener(์ด๋ฒคํŠธํƒ€์ž…, ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ)
        document.getElementById('textBtn').addEventListener('click', ()=> {
            // ํด๋ฆญ ์ด๋ฒคํŠธ ์ž˜ ๊ฑธ๋ ธ๋Š”์ง€ ํ™•์ธ!
            // console.log('text click');
            let domText = document.getElementById('container').innerText;
            console.log('๋‚ด์šฉ๋งŒ :', domText);
            console.log('๋‚ด์šฉ๋งŒ2 :', domVal.innerText);
            // ๋‚ด์šฉ ๋ณ€๊ฒฝ
            domVal.innerText = '<h1>JavaScript</h1>';
        })

        // 3. HTML ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ์š”์†Œ์—์„œ ํƒœ๊ทธ๊นŒ์ง€
        const htmlFunc = ()=>{
            // ํด๋ฆญ ์ด๋ฒคํŠธ ์ž˜ ๊ฑธ๋ ธ๋Š”์ง€ ํ™•์ธ!
            // console.log('html click');
            // ํƒœ๊ทธ๊นŒ์ง€ ๋‚ด์šฉ ๋ณ€๊ฒฝ
            domVal.innerHTML = '<h1>JavaScript</h1>'
        }
    </script>
</body>
</html>

 

โš™๏ธ HTML Collection  ๊ฐ์ฒด ๋ฐ˜ํ™˜

<!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>HTML Collection</title>
</head>
<body>
    <ul>
        <li>์ˆœ๋‘๋ถ€์ฐŒ๊ฐœ</li>
        <li>์Šคํ…Œ์ดํฌ๋Œ์†ฅ๋ฐฅ</li>
        <li>์Œˆ๋ฐฅ</li>
    </ul>

    <script>
        // getElements~ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•ด์„œ ์š”์†Œ ๊ฐ€์ ธ์˜ค๊ธฐ
        // → HTMLCollection ๊ฐ์ฒด ๋ฐ˜ํ™˜
        // ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์™€ ๊ธธ์ด๋ฅผ ๊ฐ–๋Š”๋‹ค!

        // li ์š”์†Œ๋“ค ๊ฐ€์ ธ์˜ค๊ธฐ
        let foodList = document.getElementsByTagName('li');
        console.log(foodList);
        console.log(foodList[1]);
        console.log(foodList[1].innerText);

        // ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์š”์†Œ ํ™•์ธํ•˜๊ธฐ 
        for(let i = 0 ; i < foodList.length ; i++){
            console.log(foodList[i].innerText);
        }

        // foreach ํ•จ์ˆ˜ ์‚ฌ์šฉ์‹œ ์—๋Ÿฌ ๋ฐœ์ƒ => collection์—์„œ๋Š” ๋ฐฐ์—ด ๊ด€๋ จ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค!
        // ๋ฐฐ์—ด ๊ด€๋ จ ํ•จ์ˆ˜ ์‚ฌ์šฉ ๋ถˆ๊ฐ€ → ์œ ์‚ฌ ๋ฐฐ์—ด
        foodList.forEach(element => {
            console.log(element.innerText);
        });

    </script>
</body>
</html>