10. DOM

· β˜• 3 min read · πŸ‘€... views
  1. DOM 을 λ…Έλ“œμ˜ 계측 ꡬ쑰둜 이해
  2. λ‹€μ–‘ν•œ λ…Έλ“œ νƒ€μž…
  3. λΈŒλΌμš°μ €λ“€ κ°„μ˜ λΉ„ν˜Έν™˜μ„±μ„ μš°νšŒν•˜λŠ” DOM μ½”λ”©

1. λ…Έλ“œμ˜ 계측 ꡬ쑰

  • HTML κ³Ό XML λ¬Έμ„œλŠ” λͺ¨λ‘ DOM 을 톡해 λ…Έλ“œμ˜ 계측 ꡬ쑰둜 ν‘œν˜„ κ°€λŠ₯

    1
    2
    3
    4
    5
    6
    7
    8
    
    <html>
      <head>
        <title>Sample</title>
      </head>
      <body>
        <p>Hello</p>
      </body>
    </html>
    
    • λ¬Έμ„œ λ…Έλ“œ(Document) λŠ” 각 λ¬Έμ„œλ₯Ό 루트둜 ν‘œν˜„ν•¨. μœ„μ—μ„œ λ¬Έμ„œ λ…Έλ“œμ˜ μžμ‹μ€ ν•˜λ‚˜ 뿐인데 이λ₯Ό λ¬Έμ„œμš”μ†Œ 라고 함
    • λ¬Έμ„œ μš”μ†ŒλŠ” λ¬Έμ„œμ˜ μ΅œμƒμœ„ μš”μ†Œμ΄λ©° λ‹€λ₯Έ μš”μ†ŒλŠ” λͺ¨λ‘ 이 μ•ˆμ— 쑴재
    • λ¬Έμ„œ ν•˜λ‚˜μ— λ¬Έμ„œ μš”μ†Œ ν•˜λ‚˜λ§Œ μžˆμ„ 수 있음

2. λ‹€μ–‘ν•œ λ…Έλ“œ νƒ€μž…

2-1 Node νƒ€μž…

  • DOM 레벨 1μ—λŠ” Node λΌλŠ” μΈν„°νŽ˜μ΄μŠ€κ°€ 쑴재

  • 각 λ…Έλ“œμ—λŠ” childNodes λΌλŠ” ν”„λ‘œνΌν‹°κ°€ μžˆλŠ”λ° 이 ν”„λ‘œνΌν‹°μ—λŠ” NodeList κ°€ μ €μž₯됨

  • NodeListλŠ” 처음 ν˜ΈμΆœν–ˆμ„ λ•Œ 얻은 κ²°κ³Όλ₯Ό μ €μž₯ν•˜κ³  μžˆλŠ” 것이 μ•„λ‹ˆλΌ 계속 λ°”λ€Œλ―€λ‘œ μ‚΄μ•„μžˆλŠ” 객체라고 λΆ€λ₯΄κΈ°λ„ 함

    1
    2
    3
    4
    5
    6
    
    var firstChild = someNode.childNodes[0];
    var secondChilde = someNode.childNodes.item(1);
    var count = someNode.childNodes.length; // 2
      
    someNode.childNodes.appendChild(newNode);
    console.log(count); // 3
    
  • 각 λ…Έλ“œμ—μ„œλŠ” λ¬Έμ„œ νŠΈλ¦¬μ—μ„œ λΆ€λͺ¨λ₯Ό κ°€λ₯΄ν‚€λŠ” parentNode ν”„λ‘œνΌν‹°κ°€ 있으며, childNodes λͺ©λ‘μ— 각 λ…Έλ“œλŠ” ν˜•μž¬κ΄€κ³„λ‘œ previousSibling, nextSibling 으둜 μ ‘κ·Ό κ°€λŠ₯ν•˜λ©°, λΆ€λͺ¨ μž…μž₯(parentNode) μ—μ„œλŠ” firstChild, lastChild 둜 μ ‘κ·Ό κ°€λŠ₯

  • λ…Έλ“œ μ‘°μž‘ λ©”μ†Œλ“œ

    1. appendChild() - childNodes λͺ©λ‘ λ§ˆμ§€λ§‰μ— λ…Έλ“œ μΆ”κ°€

    2. insertBefore() - childNodes νŠΉμ • μœ„μΉ˜μ— μ‚½μž…

    3. replaceChild() - κΈ°μ‘΄ λ…Έλ“œ ꡐ체

    4. removeChild() - λ…Έλ“œ μ‚­μ œ

    • replaceChild() 와 removeChild() λŠ” ν•΄λ‹Ή λ¬Έμ„œμ˜ μ†Œμœ μ΄κΈ΄ ν•˜μ§€λ§Œ λ¬Έμ„œμ—μ„œ μœ„μΉ˜λ₯Ό 지정받진 λͺ»ν•œ μƒνƒœλΌ λ³Ό 수 있음
  • cloneNode()

    1
    2
    3
    4
    5
    
    // μžμ† λ…Έλ“œ 전체 볡사
    var deepList = myList.cloneNode(true);
      
    // ν•΄λ‹Ή λ…Έλ“œ ν•˜λ‚˜λ§Œ 볡사
    var shallowList = myList.cloneNode(false);
    

2-2 Document νƒ€μž…

  • λ¬Έμ„œ λ…Έλ“œλ₯Ό Document νƒ€μž…μœΌλ‘œ ν‘œν˜„

  • window의 ν”„λ‘œνΌν‹°μ΄λ―€λ‘œ μ „μ—­μ—μ„œ μ ‘κ·Ό κ°€λŠ₯

    1
    2
    3
    4
    5
    6
    
    var html = document.documentElement; // <html>
    var body = document.body; // <body>
    var doctype = document.doctype; // <!DOCTYPE>
      
    var title = document.title; // title κ°€μ Έμ˜€κΈ°
    document.title = "new Title"; // title λ³€κ²½
    
  • μš”μ†Œ μœ„μΉ˜

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    <img src="myImage.png" name="myImage1" id="myImage">
      
    // μš”μ†Œ ID λ₯Ό λ§€κ°œλ³€μˆ˜λ‘œ λ°›μŒ
    var imgById = document.getElementById('myImage');
      
    // μš”μ†Œ tag name 을 λ§€κ°œλ³€μˆ˜λ‘œ λ°›μŒ
    var imgByTag = document.getElementByTagName('img');
      
    // μš”μ†Œ name 을 λ§€κ°œλ³€μˆ˜λ‘œ λ°›μŒ
    var imgByName = document.getElementByName('myImage1');
    
  • λ¬Έμ„œμ— μ“°κΈ°

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    <html>
      <head>
        <title>document.write() Example</title>
      </head>
      <body>
        <script type="text/javascript">
          document.write("<script type=\"text/javascript\" src=\"file.js\">" + "<\/script>");
        </script>
      </body>
    </html>
    

2-3 Element νƒ€μž…

  • HTML μš”μ†Œ

    • Id - μš”μ†Œμ˜ 고유 μ‹λ³„μž
    • title - μš”μ†Œμ˜ μΆ”κ°€μ •λ³΄λ‘œ 일반적으둜 툴팁으둜 ν‘œν˜„λ¨
    • classNmae - μš”μ†Œμ˜ CSS 클래슀인 class 속성
  • 속성 μ–»κΈ°

    • getAttribute()

    • setAttribute()

    • removeAttribute()

    • 주둜 μ»€μŠ€ν…€ 속성을 λ‹€λ£° λ•Œ μ‚¬μš©
    1
    2
    
    div.myColor = "blue";
    console.log(div.getAttribute("myColor")); // null
    
    • λŒ€λΆ€λΆ„μ˜ λΈŒλΌμš°μ €μ—μ„œ 이 ν”„λ‘œνΌν‹°λŠ” μžλ™μœΌλ‘œ μš”μ†Œ 속성이 λ˜μ§€λŠ” μ•ŠμœΌλ―€λ‘œ getAttribute()둜 같은 μ΄λ¦„μ˜ 속성에 μ ‘κ·Όν•˜λ©΄ null 을 λ°˜ν™˜
  • μš”μ†Œ 생성

    1
    2
    3
    4
    5
    6
    
    var div = document.createElement('div');
    div.id = "myNewDiv";
    div.className = "box";
      
    // μƒμ„±ν•œ μš”μ†Œλ₯Ό λ¬Έμ„œ νŠΈλ¦¬μ— μΆ”κ°€ν•΄μ•Ό 반영
    document.body.appendChild(div);
    

2-4 Text νƒ€μž…

  • ν‰λ²”ν•œ ν…μŠ€νŠΈκ°€ ν¬ν•¨λ˜κ³ , κΈ€μž κ·ΈλŒ€λ‘œ μ‚¬μš©λ¨

  • μ΄μŠ€μΌ€μ΄ν”„λœ HTML λ¬ΈμžλŠ” 포함할 수 μžˆμ§€λ§Œ, HTML μ½”λ“œλŠ” 포함 λΆˆκ°€λŠ₯

    1
    2
    3
    4
    5
    
    var textNode = document.createTextNode("<strong>hello</strong> wolrd");
      
    element.appendChild(textNode);
      
    document.body.appendChild(element);
    

2-5 Comment νƒ€μž…

  • 주석은 DOM μ—μ„œ Comment νƒ€μž…μœΌλ‘œ ν‘œν˜„

    1
    
    var comment = document.createComment("comment");
    

2-6 CDATASection νƒ€μž…

  • XML 기반 λ¬Έμ„œ μ „μš© (myBatisλ₯Ό μ‚¬μš©ν•˜λ©΄ 자주 λ΄„)

    1
    
    <div id="myDiv"><![CDATA[this is some content.]]></div>
    

2-7 DocumentType νƒ€μž…

  • λ¬Έμ„œμ˜ λ…νƒ€μž… 정보

    <!DOCTYPE HTML PUBLIC "-//W3C" ~~~~>
      
    console.log(document.doctype.name); // HTML
    

3. DOM 닀루기

  • DOM μ‘°μž‘μ€ λ‹¨μˆœν•˜λ©°, μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό μ“Έ λ•Œλ„ 일반적인 HTML μ½”λ“œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ ν•˜λ©΄ 됨 (μ–Έμ œλ‚˜ μ˜ˆμ™ΈλŠ” μžˆμ§€λ§Œ..)

3-1 동적 μžλ°”μŠ€ν¬λ¦½νŠΈ

  • 1
    
    <script type="text/javascript" src="client.js" />
    
  • 1
    2
    3
    4
    5
    6
    7
    8
    
    function loadScript(url) {
      var script = document.createElement('script');
      script.type = "text/javascript";
      script.src = url;
      document.body.appendChild(script);
    }
      
    loadScript("client.js")
    

3-2 동적 μŠ€νƒ€μΌ

  • 1
    
    <link rel="stylesheet" type="text/css" href="styles.css">
    
  •  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    function loadStyles(url) {
      var link = document.createElement('link');
      link.rel = "stylesheet";
      link.type = "text/css";
      link.href = "style.css";
      var head = document.getElementByTagName('head')[0];
      head.appendChild(link);
    }
      
    loadStyles("styles.css");
    

3-3 λ…Έλ“œλ¦¬μŠ€νŠΈ μ‚¬μš©

  • μ‚΄μ•„μžˆλŠ” 객체둜 κ°„μ£Όλ˜κΈ°μ— λ¬΄ν•œ 루프가 될 수 있음

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    var divs = document.getElementByTagName('div'),
        i,
        div;
      
    for(i = 0; i < divs.length; i++) {
      div = document.createElement('div');
      document.body.appendChild(div);
    }
    // 루프가 반볡될 λ•Œλ§ˆλ‹€ i < divs.length 쑰건을 ν™•μΈν•˜λŠ”λ°
    // divs.length κ°€ 계속 κ°±μ‹ λ˜λ―€λ‘œ λ¬΄ν•œ 루프
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    var divs = document.getElementByTagName('div'),
        i,
        len,
        div;
      
    for(i = 0, len = divs.length; i < len; i++) {
      div = document.createElement('div');
      document.body.appendChild(div);
    }
    
  • μ»¬λ ‰μ…˜μ— μ ‘κ·Όν•  λ•Œλ§ˆλ‹€ λ‹€μ‹œ 쿼리 ν•˜λ―€λ‘œ, 일반적으둜 NodeList μžμ²΄μ— μ ‘κ·Όν•˜λŠ” 일은 λ˜λ„λ‘ ν”Όν•˜λŠ” 것이 μ’‹μŒ. μ‚¬μš©ν•œλ‹€λ©΄ λ³€μˆ˜μ— μ €μž₯ν•˜μ—¬ μ‚¬μš©

Share on

snack
WRITTEN BY
snack
Web Programmer


What's on this Page