- ๋ธ๋ผ์ฐ์ ํ๋ฉด์ ๋์ ์ผ๋ก ๋ง๋ค๊ธฐ ์ํจ
- ๋ธ๋ผ์ฐ์ ๋ฅผ ์กฐ์ํ ์ ์๋ ์ ์ผํ ์ธ์ด
DOM ์ด๋?
- HTML, XML๊ณผ ๊ฐ์ ๋ฌธ์๋ฅผ ๋ค๋ฃจ๊ธฐ ์ํ ๋ฌธ์ ํ๋ก๊ทธ๋๋ฐ ์ธํฐํ์ด์ค
- ๋ฌธ์๋ฅผ ๊ตฌ์กฐํํ๊ณ ๊ตฌ์กฐํ๋ ๊ตฌ์ฑ ์์๋ฅผ ํ๋์ ๊ฐ์ฒด๋ก ์ทจ๊ธํ์ฌ ๋ค๋ฃจ๋ ๋ ผ๋ฆฌ์ ํธ๋ฆฌ ๋ชจ๋ธ
- ๋ฌธ์๊ฐ ๊ตฌ์กฐํ๋์ด ์์ผ๋ฉฐ ๊ฐ ์์๋ ๊ฐ์ฒด(object)๋ก ์ทจ๊ธ
- ๋จ์ํ ์์ฑ ์ ๊ทผ, ๋ฉ์๋ ํ์ฉ๋ฟ๋ง ์๋๋ผ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ํน์ฑ์ ํ์ฉํ ์กฐ์ ๊ฐ๋ฅ
- ์ฃผ์ ๊ฐ์ฒด
- window : DOM์ ํํํ๋ ์ฐฝ. ๊ฐ์ฅ ์ต์์ ๊ฐ์ฒด (์์ฑ ์ ์๋ต ๊ฐ๋ฅ)
- document : ํ์ด์ง ์ปจํ ์ธ ์ Entry Point ์ญํ ์ ํ๋ฉฐ, ๋ฑ๊ณผ ๊ฐ์ ์๋ง์ ๋ค๋ฅธ ์์๋ค์ ํฌํจ
- navigator, location, history, screen
ํ์ฑ(Parsing)
- ๊ตฌ๋ฌธ ๋ถ์, ํด์
- ๋ธ๋ผ์ฐ์ ๊ฐ ๋ฌธ์์ด์ ํด์ํ์ฌ DOM Tree๋ก ๋ง๋๋ ๊ณผ์
BOM(browser Object Model) ์ด๋?
- ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ๋ธ๋ผ์ฐ์ ์ ์ํตํ๊ธฐ ์ํ ๋ชจ๋ธ
- ๋ธ๋ผ์ฐ์ ์ ์ฐฝ์ด๋ ํ๋ ์์ ์ถ์ํํด์ ํ๋ก๊ทธ๋๋ฐ์ ์ผ๋ก ์ ์ดํ ์ ์๋๋ก ์ ๊ณตํ๋ ์๋จ
- ๋ฒํผ, URL ์ ๋ ฅ์ฐฝ, ํ์ดํ ๋ฐ ๋ฑ ๋ธ๋ผ์ฐ์ ์๋์ฐ ๋ฐ ์น ํ์ด์ง ์ผ๋ถ๋ถ์ ์ ์ด ๊ฐ๋ฅ
- window ๊ฐ์ฒด๋ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ๋ก๋ถํฐ ์ง์๋ฐ์ผ๋ฉฐ ๋ธ๋ผ์ฐ์ ์ ์ฐฝ(window)๋ฅผ ์ง์นญ
JavaScript Core
- ํ๋ก๊ทธ๋๋ฐ ์ธ์ด
DOM ์กฐ์ - ๊ฐ๋
- Document๋ ๋ฌธ์ ํ ์ฅ(HTML)์ ํด๋นํ๊ณ ์ด๋ฅผ ์กฐ์
- DOM ์กฐ์ ์์
- ์ ํ (Select)
- ๋ณ๊ฒฝ (Manipulation)
DOM ์กฐ์ - Document ์์น
DOM ๊ด๋ จ ๊ฐ์ฒด์ ์์ ๊ตฌ์กฐ
EventTarget
- Event Listener๋ฅผ ๊ฐ์ง ์ ์๋ ๊ฐ์ฒด๊ฐ ๊ตฌํํ๋ DOM ์ธํฐํ์ด์ค
Node
- ์ฌ๋ฌ ๊ฐ์ง DOM ํ์ ๋ค์ด ์์ํ๋ ์ธํฐํ์ด์ค
Element
- Document ์์ ๋ชจ๋ ๊ฐ์ฒด๊ฐ ์์ํ๋ ๊ฐ์ฅ ๋ฒ์ฉ์ ์ธ ๊ธฐ๋ฐ ํด๋์ค
- ๋ถ๋ชจ์ธ Node์ ๊ทธ ๋ถ๋ชจ์ธ EventTarget์ ์์ฑ์ ์์
Document
- ๋ธ๋ผ์ฐ์ ๊ฐ ๋ถ๋ฌ์จ ์น ํ์ด์ง๋ฅผ ๋ํ๋
- DOM ํธ๋ฆฌ์ ์ง์ ์ (entry point) ์ญํ ์ ์ํ
HTMLElement
- ๋ชจ๋ ์ข ๋ฅ์ HTML ์์
- ๋ถ๋ชจ element์ ์์ฑ ์์
DOM ์ ํ - ์ ํ ๊ด๋ จ ๋ฉ์๋
- Document.querySelector(selector)
- ์ ๊ณตํ ์ ํ์์ ์ผ์นํ๋ element ํ๋ ์ ํ
- ์ ๊ณตํ CSS selector๋ฅผ ๋ง์กฑํ๋ ์ฒซ ๋ฒ์งธ element ๊ฐ์ฒด๋ฅผ ๋ฐํ (์๋ค๋ฉด null)
- Document.querySelectorAll(selector)
- ์ ๊ณตํ ์ ํ์์ ์ผ์นํ๋ ์ฌ๋ฌ element๋ฅผ ์ ํ
- ๋งค์นญ ํ ํ๋ ์ด์์ ์ ๋ ํฐ๋ฅผ ํฌํจํ๋ ์ ํจํ CSS selector๋ฅผ ์ธ์(๋ฌธ์์ด)๋ก ๋ฐ์
- ์ง์ ๋ ์ ๋ ํฐ์ ์ผ์นํ๋ NodeList๋ฅผ ๋ฐํ
- getElementById(id)
- getElementsByTagName(name)
- getElementsByClassName(names)
โป querySelector(), querySelectorAll() ์ ์ฌ์ฉํ๋ ์ด์
- id, class ๊ทธ๋ฆฌ๊ณ tag ์ ํ์ ๋ฑ์ ๋ชจ๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ฏ๋ก, ๋ ๊ตฌ์ฒด์ ์ด๊ณ ์ ์ฐํ๊ฒ ์ ํ ๊ฐ๋ฅ
- ex) document.querySelector('#id'), document.querySelectAll('.name')
DOM ์ ํ - ์ ํ ๋ฉ์๋๋ณ ๋ฐํ ํ์
- ๋จ์ผ element
- getElementById()
- querySelector()
- HTMLCollection
- getElementsByTagName()
- getElementsByClassName()
- NodeList
- querySelectorAll()
DOM ์ ํ - HTMLCollection & NodeList
- ๋ ๋ค ๋ฐฐ์ด๊ณผ ๊ฐ์ด ๊ฐ ํญ๋ชฉ์ ์ ๊ทผํ๊ธฐ ์ํ index๋ฅผ ์ ๊ณต(์ ์ฌ ๋ฐฐ์ด)
- HTMLCollection
- name, id, index ์์ฑ์ผ๋ก ๊ฐ ํญ๋ชฉ์ ์ ๊ทผ ๊ฐ๋ฅ
- NodeList
- index๋ก๋ง ๊ฐ ํญ๋ชฉ์ ์ ๊ทผ ๊ฐ๋ฅ
- ๋จ, HTMLCollection๊ณผ ๋ฌ๋ฆฌ ๋ฐฐ์ด์์ ์ฌ์ฉํ๋ forEach ํจ์ ๋ฐ ๋ค์ํ ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
- ๋ ๋ค Live Collection์ผ๋ก DOM์ ๋ณ๊ฒฝ์ฌํญ์ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ์ง๋ง, querySelectorAll()์ ์ํด ๋ฐํ๋๋ NodeList๋ Static Collection์ผ๋ก ์ค์๊ฐ์ผ๋ก ๋ฐ์๋์ง ์์
DOM ์ ํ - Collection
- Live Collection
- ๋ฌธ์๊ฐ ๋ฐ๋ ๋ ์ค์๊ฐ์ผ๋ก ์ ๋ฐ์ดํธ ๋จ
- DOM์ ๋ณ๊ฒฝ์ฌํญ์ ์ค์๊ฐ์ผ๋ก collection์ ๋ฐ์
- ex) HTMLCollection, NodeList
- Static Collection (non-live)
- DOM์ด ๋ณ๊ฒฝ๋์ด๋ collection ๋ด์ฉ์๋ ์ํฅ์ ์ฃผ์ง ์์
- querySelectorAll()์ ๋ฐํ NodeList๋ง static collection
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>
.city-location {color: blue;}</style></head><body><h1>Hello City</h1><h2id="location-header">Location</h2><div><ul><liclass="city-location">์์ธ</li><liclass="city-location">๋์ </li><liclass="city-location">๊ด์ฃผ</li><liclass="city-location">๊ตฌ๋ฏธ</li><liclass="city-location">๋ถ์ธ๊ฒฝ</li></ul></div></body></html>// 1. Selection// 1-1. window & documentconsole.log(window)console.log(document)console.log(window.document)// 1-2. querySelectorconsth1=document.querySelector('h1')consth2=document.querySelector('h2')constsecondH2=document.querySelector('#location-header')constselectUlTag=document.querySelector('div > ul')// 1-3. querySelectorAllconstliTags=document.querySelectorAll('li')constsecondLiTags=document.querySelectorAll('.city-location')// 2. Creation & Append// 2-1. createElementconstulTag=document.querySelector('ul')constnewLiTag=document.createElement('li')// 2-2. appendnewLiTag.innerText='์๋ก์ด ๋ฆฌ์คํธ ํ๊ทธ'ulTag.append(newLiTag)ulTag.append('๋ฌธ์์ด๋ ์ถ๊ฐ ๊ฐ๋ฅ')constnew1=document.createElement('li')new1.innerText='๋ฆฌ์คํธ 1'constnew2=document.createElement('li')new2.innerText='๋ฆฌ์คํธ 2'constnew3=document.createElement('li')new3.innerText='๋ฆฌ์คํธ 3'ulTag.append(new1,new2,new3)// 2-3. appendChildconstulTag=document.querySelector('ul')constnewLiTag=document.createElement('li')newLiTag.innerText='์๋ก์ด ๋ฆฌ์คํธ ํ๊ทธ'ulTag.appendChild(newLiTag)ulTag.appendChild('๋ฌธ์์ด์ ์ถ๊ฐ ๋ถ๊ฐ')constnew1=document.createElement('li')new1.innerText='๋ฆฌ์คํธ 1'constnew2=document.createElement('li')new2.innerText='๋ฆฌ์คํธ 2'ulTag.appendChild(new1,new2)// 2-4. innerText & innerHTMLconstulTag=document.querySelector('ul')constliTag1=document.createElement('li')liTag1.innerText='<li>์ถ์ฒ</li>'constliTag2=document.createElement('li')liTag2.innerHTML='<li>์ถ์ฒ</li>'ulTag.append(liTag1,liTag2)constulTag=document.querySelector('ul')ulTag.innerHTML='<li><a href="javascript:alert(\'๋น์ ์ ๊ฐ์ธ์ ๋ณด ์ ์ถ\')">์ถ์ฒ</a></li>'// 3. Delete// 3-1. removeconstheader=document.querySelector('#location-header')header.remove()// 3-2. removeChildconstparent=document.querySelector('ul')constchild=document.querySelector('ul > li')constremovedChild=parent.removeChild(child)console.log(removedChild)// 4. ์์ฑ// 4-1. setAttributeconstheader=document.querySelector('#location-header')header.setAttribute('class','city-location')// 4-2. getAttributeconstgetAttr=document.querySelector('.city-location')getAttr.getAttribute('class')getAttr.getAttribute('style')

















