CodeGym /ํ–‰๋™ /Frontend SELF KO /์†์„ฑ๊ณผ ์Šคํƒ€์ผ ์ž‘์—…

์†์„ฑ๊ณผ ์Šคํƒ€์ผ ์ž‘์—…

Frontend SELF KO
๋ ˆ๋ฒจ 41 , ๋ ˆ์Šจ 3
์‚ฌ์šฉ ๊ฐ€๋Šฅ

4.1 ์†์„ฑ ์กฐ์ž‘

DOM ์š”์†Œ์˜ ์†์„ฑ๊ณผ ์Šคํƒ€์ผ์„ ์กฐ์ž‘ํ•˜๋Š” ๊ฒƒ์€ ์›น ๋ฌธ์„œ ์ž‘์—…์˜ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด์•ผ. ์ด๊ฑธ ํ†ตํ•ด ์‚ฌ์šฉ์ž ํ–‰๋™์ด๋‚˜ ๋‹ค๋ฅธ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์š”์†Œ์˜ ์™ธ๊ด€๊ณผ ํ–‰๋™์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์–ด.

1. ์†์„ฑ ์–ป๊ธฐ

์š”์†Œ์˜ ์†์„ฑ ๊ฐ’์„ ์–ป์œผ๋ ค๋ฉด getAttribute() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
        </head>
        <body>
          <a href="https://example.com" id="myLink">Visit Example.com</a>

          <script>
            const link = document.getElementById('myLink');
            const href = link.getAttribute('href');
            console.log(href); // ์ถœ๋ ฅ: https://example.com
          </script>
        </body>
      </html>
    
  

2. ์†์„ฑ ์„ค์ •

์†์„ฑ์˜ ๊ฐ’์„ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด setAttribute() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
        </head>
        <body>
          <a href="https://example.com" id="myLink">Visit Example.com</a>

          <script>
            const link = document.getElementById('myLink');
            link.setAttribute('href', 'https://newexample.com');
            console.log(link.getAttribute('href')); // ์ถœ๋ ฅ: https://newexample.com
          </script>
        </body>
      </html>
    
  

3. ์†์„ฑ ์‚ญ์ œ

์†์„ฑ์„ ์‚ญ์ œํ•˜๋ ค๋ฉด removeAttribute() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
        </head>
        <body>
          <a href="https://example.com" id="myLink" target="_blank">Visit Example.com</a>

          <script>
            const link = document.getElementById('myLink');
            link.removeAttribute('target');
            console.log(link.getAttribute('target')); // ์ถœ๋ ฅ: null
          </script>
        </body>
      </html>
    
  

4. ์†์„ฑ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ

์†์„ฑ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด hasAttribute() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
        </head>
        <body>
          <a href="https://example.com" id="myLink" target="_blank">Visit Example.com</a>

          <script>
            const link = document.getElementById('myLink');
            console.log(link.hasAttribute('target')); // ์ถœ๋ ฅ: true
            link.removeAttribute('target');
            console.log(link.hasAttribute('target')); // ์ถœ๋ ฅ: false
          </script>
        </body>
      </html>
    
  

4.2 ํด๋ž˜์Šค ์กฐ์ž‘

1. ํด๋ž˜์Šค ์ถ”๊ฐ€

์š”์†Œ์— ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด classList.add() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
          <style>
            .highlight {
              background-color: yellow;
            }
          </style>
        </head>
        <body>
          <p id="myParagraph">This is a paragraph.</p>

          <script>
            const paragraph = document.getElementById('myParagraph');
            paragraph.classList.add('highlight');
          </script>
        </body>
      </html>
    
  

2. ํด๋ž˜์Šค ์‚ญ์ œ

์š”์†Œ์—์„œ ํด๋ž˜์Šค๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด classList.remove() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
          <style>
            .highlight {
              background-color: yellow;
            }
          </style>
        </head>
        <body>
          <p id="myParagraph" class="highlight">This is a paragraph.</p>

          <script>
            const paragraph = document.getElementById('myParagraph');
            paragraph.classList.remove('highlight');
          </script>
        </body>
      </html>
    
  

3. ํด๋ž˜์Šค ํ† ๊ธ€

ํด๋ž˜์Šค๋ฅผ ํ† ๊ธ€ํ•˜๋ ค๋ฉด classList.toggle() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด. ํด๋ž˜์Šค๊ฐ€ ์—†๋‹ค๋ฉด ์ถ”๊ฐ€ํ•˜๊ณ , ์žˆ์œผ๋ฉด ์‚ญ์ œํ•ด์ค˜.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
          <style>
            .highlight {
              background-color: yellow;
            }
          </style>
        </head>
        <body>
          <button type="button" id="myButton">Click me!</button>

          <script>
            const btn = document.getElementById('myButton');
            btn.addEventListener("click", () => {
              btn.classList.toggle('highlight'); // ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€. ๋‹ค์‹œ ๋ˆ„๋ฅด๋ฉด ํด๋ž˜์Šค๋ฅผ ์‚ญ์ œ.
            });
          </script>
        </body>
      </html>
    
  

4. ํด๋ž˜์Šค ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ

ํด๋ž˜์Šค์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด classList.contains() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
          <style>
            .highlight {
              background-color: yellow;
            }
          </style>
        </head>
        <body>
          <p id="myParagraph" class="highlight">This is a paragraph.</p>

          <script>
            const paragraph = document.getElementById('myParagraph');
            console.log(paragraph.classList.contains('highlight')); // ์ถœ๋ ฅ: true
            paragraph.classList.remove('highlight');
            console.log(paragraph.classList.contains('highlight')); // ์ถœ๋ ฅ: false
          </script>
        </body>
      </html>
    
  

4.3 ์Šคํƒ€์ผ ์กฐ์ž‘

1. ์ธ๋ผ์ธ ์Šคํƒ€์ผ ๋ณ€๊ฒฝ

์š”์†Œ์˜ ์ธ๋ผ์ธ ์Šคํƒ€์ผ์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด style ์†์„ฑ์„ ์จ.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
        </head>
        <body>
          <p id="myParagraph">This is a paragraph.</p>

          <script>
            const paragraph = document.getElementById('myParagraph');
            paragraph.style.color = 'red';
            paragraph.style.fontSize = '20px';
          </script>
        </body>
      </html>
    
  

2. ๊ณ„์‚ฐ๋œ ์Šคํƒ€์ผ ์–ป๊ธฐ

์š”์†Œ์˜ ํ˜„์žฌ ๊ณ„์‚ฐ๋œ ์Šคํƒ€์ผ์„ ์–ป์œผ๋ ค๋ฉด window.getComputedStyle() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
          <style>
            #myParagraph {
              color: blue;
              font-size: 18px;
            }
          </style>
        </head>
        <body>
          <p id="myParagraph">This is a paragraph.</p>

          <script>
            const paragraph = document.getElementById('myParagraph');
            const styles = window.getComputedStyle(paragraph);
            console.log(styles.color); // ์ถœ๋ ฅ: rgb(0, 0, 255) ๋˜๋Š” ํŒŒ๋ž€์ƒ‰
            console.log(styles.fontSize); // ์ถœ๋ ฅ: 18px
          </script>
        </body>
      </html>
    
  

3. ์ธ๋ผ์ธ ์Šคํƒ€์ผ ์‚ญ์ œ

์ธ๋ผ์ธ ์Šคํƒ€์ผ์„ ์‚ญ์ œํ•˜๋ ค๋ฉด ์Šคํƒ€์ผ ๊ฐ’์„ ๋นˆ ๋ฌธ์ž์—ด๋กœ ์„ค์ •ํ•˜๋ฉด ๋ผ.

์˜ˆ:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document</title>
        </head>
        <body>
          <p id="myParagraph" style="color: red; font-size: 20px;">This is a paragraph.</p>

          <script>
            const paragraph = document.getElementById('myParagraph');
            paragraph.style.color = '';
            paragraph.style.fontSize = '';
          </script>
        </body>
      </html>
    
  
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION