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

JS_๋ฌธ๋ฒ• (8)_DOM CRUD ํŒŒํ—ค์น˜๊ธฐ

by KASSID 2022. 2. 24.

๋ชฉ์ฐจ

    728x90

    Create Read Update Delete

     

    HTML ์ฐพ๊ธฐ

    - doucument.getElementById(id๋ช…) : id๋กœ ์ฐพ๊ธฐ

    - doucument.getElementsByTagName(ํƒœ๊ทธ๋ช…) : ํƒœ๊ทธ๋กœ ์ฐพ๊ธฐ(๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜)

    - doucument.getElementsByClassName(ํด๋ž˜์Šค๋ช…) : ํด๋ž˜์Šค๋กœ ์ฐพ๊ธฐ(๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜)

     

    - document.querySelector(css ์„ ํƒ์ž) : css ์„ ํƒ์ž๋กœ ์ฐพ๊ธฐ(์ตœ์ดˆ๋“ฑ์žฅ ํ•˜๋‚˜๋งŒ ๋ฐ˜ํ™˜)

    - document.querySelectorAll(css ์„ ํƒ์ž) : css ์„ ํƒ์ž๋กœ ์ฐพ๊ธฐ(๋ชจ๋“  ์š”์†Œ. ๋ฐฐ์—ด ํ˜•ํƒœ์˜ ๊ฐ์ฒด(์œ ์‚ฌ๋ฐฐ์—ด)๋กœ ๋ฐ˜ํ™˜)

    (์ธ์ˆ˜๋กœ css ์„ ํƒ์ž๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.  ' . '(class), ' # '(id), ' * '(all), (tag๋Š” prefix๋ถ™์ด์ง€ ์•Š์Œ) ๋“ฑ์˜ ์กฐํ•ฉ)

    ์˜ˆ์‹œ

    id๋ช…์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ

    console.log(document.getElementById('root'))
    
    >>>
    <div id='root'>...</div>

     

    ํด๋ž˜์Šค๋ช…์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ (๋ฐฐ์—ด)

    console.log(document.getElementsByClassName('el')) //Element's'์ด๋ฉด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
    
    >>>
    HTMLCollection(17) [div.el, div.el, div.el,
    div.el, div.el, div.el, div.el, div.el, div.el,
    div.el, div.el, div.el.e_el__dashboard, div.el.e_el__courses,
    div.el.e_el__home, div.el.e_el__roadmaps, div.el.e_el__more,
    span.el.cart_modal_btn.e_cart_modal_btn.nav-modal-btn]
    // 17๊ฐœ item ๊ฐ€์ง„ ๋ฐฐ์—ด์ด๋ผ๋Š” ๋œป

     

    CSS ์„ ํƒ์ž๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ

    console.log(document.querySelectorAll('a.nav'))
    
    >>> //NodeList ๋ผ๋Š” ๊ฐ์ฒด ๋ฐ˜ํ™˜
    NodeList(16) [a.nav, a.nav, a.nav, a.nav,
    a.nav.shop, a.nav.shoplive, a.nav, a.nav,
    a.nav, a.nav, a.nav, a.nav, a.nav, a.nav, a.nav, a.nav]

     

    HTML ์š”์†Œ ํ™•์ธ & ์ˆ˜์ •

    ์„ ํƒ์ž๋ฅผ ์ด์šฉํ•ด ๊ฐ€์ ธ์˜จ ์š”์†Œ ์ ‘๊ทผ

     

    - ์š”์†Œ.innerText : ์š”์†Œ ํ™•์ธ (๋‚ด์šฉ๋งŒ)

    - ์š”์†Œ.innerHTML : ํƒœ๊ทธ๊นŒ์ง€ ํฌํ•จ

    - ์š”์†Œ.attribute : ์š”์†Œ ์†์„ฑ ๊ฐ’

    - ์š”์†Œ.style.property : ์š”์†Œ์˜ CSS ํ”„๋กœํผํ‹ฐ ๊ฐ’

     

    ์˜ˆ์‹œ

    innerText

    ๋‚ด์šฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    element = document.querySelectorAll('li.nav_item')
    
    for (let i=0; i<element.length; i++){
        console.log(element[i].innerText)
    }
    
    
    >>>
    ๋ฉ”์ผ
    ์นดํŽ˜
    ๋ธ”๋กœ๊ทธ
    ์ง€์‹iN
    ์‡ผํ•‘
    ์‡ผํ•‘LIVE
    Pay
    TV
    ์‚ฌ์ „
    ๋‰ด์Šค
    ์ฆ๊ถŒ
    ๋ถ€๋™์‚ฐ
    ์ง€๋„
    VIBE
    ์ฑ…
    ์›นํˆฐ

    innerHTML

    ํƒœ๊ทธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    element = document.querySelectorAll('li.nav_item')
    
    for (let i=0; i<element.length; i++){
        console.log(element[i].innerHTML)
    }
    
    >>>
    <a href="https://mail.naver.com/" class="nav" data-clk="svc.mail"><i class="ico_mail"></i>๋ฉ”์ผ</a>
    
    <a href="https://section.cafe.naver.com/" class="nav" data-clk="svc.cafe">์นดํŽ˜</a>
    <a href="https://section.blog.naver.com/" class="nav" data-clk="svc.blog">๋ธ”๋กœ๊ทธ</a>
    <a href="https://kin.naver.com/" class="nav" data-clk="svc.kin">์ง€์‹iN</a>
    <a href="https://shopping.naver.com/" class="nav shop" data-clk="svc.shopping"><span class="blind">์‡ผํ•‘</span></a>
    <a href="https://shoppinglive.naver.com/home" class="nav shoplive" data-clk="svc.shoppinglive"><span class="blind">์‡ผํ•‘LIVE</span></a>
    <a href="https://order.pay.naver.com/home" class="nav" data-clk="svc.pay">Pay</a>
    <a href="https://tv.naver.com/" class="nav" data-clk="svc.tvcast"><i class="ico_tv"></i>TV</a>
    
    <a href="https://dict.naver.com/" class="nav" data-clk="svc.dic">์‚ฌ์ „</a>
    <a href="https://news.naver.com/" class="nav" data-clk="svc.news">๋‰ด์Šค</a>
    <a href="https://finance.naver.com/" class="nav" data-clk="svc.stock">์ฆ๊ถŒ</a>
    <a href="https://land.naver.com/" class="nav" data-clk="svc.land">๋ถ€๋™์‚ฐ</a>
    <a href="https://map.naver.com/" class="nav" data-clk="svc.map">์ง€๋„</a>
    <a href="https://vibe.naver.com/" class="nav" data-clk="svc.vibe">VIBE</a>
    <a href="https://book.naver.com/" class="nav" data-clk="svc.book">์ฑ…</a>
    <a href="https://comic.naver.com/" class="nav" data-clk="svc.webtoon">์›นํˆฐ</a>

    ์ด๋ฒˆ์—๋Š” ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž

     

    HTML ์š”์†Œ ์ƒ์„ฑ/์ถ”๊ฐ€

    document.createElement(ํƒœ๊ทธ๋ช…) : HTML ์š”์†Œ ์ƒ์„ฑ

    document.appendChild(์š”์†Œ) : 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>Document</title>
        <style>
            .box1 {
                width: 100px;
                height: 100px;
                border-radius: 5px;
                line-height: 100px;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <script>
            const newBox1 = document.createElement("div") //div ์š”์†Œ ์ƒ์„ฑ
            newBox1.innerText = "Hello" //๋ฌธ๊ตฌ ์ถ”๊ฐ€
            newBox1.setAttribute("class","box1") //box1์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํด๋ž˜์Šค ์„ค์ •
            newBox1.style.background = "blueviolet" //backgroundํ”„๋กœํผํ‹ฐ ์ ์šฉ
            document.body.appendChild(newBox1) //์š”์†Œ ์ถ”๊ฐ€
        </script>
    </body>
    </html>

     

    ๋ธ”๋ก์„ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต!


    HTML ์š”์†Œ ์‚ญ์ œ/๋Œ€์ฒด

    document.removeChild(์š”์†Œ) : HTML ์š”์†Œ ์‚ญ์ œ

    document.replaceChile( ์ƒˆ๋กœ์šด ๊ฒƒ, ๊ธฐ์กด์˜ ๊ฒƒ ) : 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>Document</title>
        <style>
            .box1 {
                width: 100px;
                height: 100px;
                border-radius: 5px;
                line-height: 100px;
                text-align: center;
            }
            .box2 {
                width: 200px;
                height: 200px;
                border-radius: 100px;
                line-height: 200px;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <script>
            const newBox1 = document.createElement("div");
            newBox1.innerText = "Hello";
            newBox1.setAttribute("class","box1");
            newBox1.style.background = "blueviolet";
            
            const newBox2 = document.createElement("div");
            newBox2.innerText = "World";
            newBox2.setAttribute("class","box2");
            newBox2.style.background = "blueviolet";
            
            document.body.appendChild(newBox1);
            document.body.replaceChild(newBox2, newBox1); // ๋Œ€์ฒดํ•˜๊ธฐ
        </script>
    </body>
    </html>

    ๋ธ”๋ก์ด ๋Œ€์ฒด๋œ ๋ชจ์Šต!


    HTML ์š”์†Œ ํƒ์ƒ‰

     

    element.parentNode : ๋ถ€๋ชจ์š”์†Œ

    element.nextElementSibling : ํ˜„์žฌ ์š”์†Œ์˜ ๋‹ค์Œ ํ˜•์ œ ์š”์†Œ

    element.previousElementSibling : ํ˜„์žฌ ์š”์†Œ์˜ ์ด์ „ ํ˜•์ œ ์š”์†Œ

    element.children : ์ž์‹ ์š”์†Œ๋“ค(๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜)โ€‹

    ๋Œ“๊ธ€