๐โโ๏ธ DOM(Document Object Model)์ด๋,
HTML ๋ฌธ์๋ฅผ ํ์ฑํ์ฌ ๋ธ๋ผ์ฐ์ ๊ฐ ์ดํดํ ์ ์๋ ์๋ฃ ๊ตฌ์กฐ
- ์๋ฐ์คํฌ๋ฆฝํธ์์ DOM API๋ฅผ ์ฌ์ฉํ๋ฉด ์ด๋ฏธ ์์ฑ ๋ DOM์ ๋์ ์ผ๋ก ์กฐ์ ๊ฐ๋ฅ
๐โโ๏ธ ๋ธ๋ผ์ฐ์ ๋ ๋๋ง(browser rendering)์ด๋,
๋ธ๋ผ์ฐ์ ๋ HTML, CSS, ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์์ฑ ๋ ํ ์คํธ ๋ฌธ์๋ฅผ ํ์ฑํ์ฌ ๋ธ๋ผ์ฐ์ ๋ ๋๋ง
๐โโ๏ธ ํ์ฑ(parsing)์ด๋,
ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๋ฌธ๋ฒ์ ๋ง๊ฒ ์์ฑ ๋ ํ ์คํธ ๋ฌธ์๋ฅผ ์ฝ์ด ๋ค์ฌ ์คํํ๊ธฐ ์ํด ํ ์คํธ ๋ฌธ์์ ๋ฌธ์์ด์ ํ ํฐ์ผ๋ก ๋ถํดํ๊ณ , ํ ํฐ์ ๋ฌธ๋ฒ์ ์๋ฏธ์ ๊ตฌ์กฐ๋ฅผ ๋ฐ์ํ์ฌ ํธ๋ฆฌ ๊ตฌ์กฐ์ ์๋ฃ ๊ตฌ์กฐ์ธ ํ์ค ํธ๋ฆฌ(parse tree)๋ฅผ ์์ฑํ๋ ์ผ๋ จ์ ๊ณผ์
๐โโ๏ธ ๋ ๋๋ง(rendering)์ด๋,
HTML, CSS, ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์์ฑ ๋ ๋ฌธ์๋ฅผ ํ์ฑํ์ฌ ๋ธ๋ผ์ฐ์ ์ ์๊ฐ์ ์ผ๋ก ์ถ๋ ฅํ๋ ๊ฒ
ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๋ฌธ๋ฒ์ ๋ง๊ฒ ์์ฑ ๋ ํ ์คํธ ๋ฌธ์๋ฅผ ์ฝ์ด ๋ค์ฌ ์คํํ๊ธฐ ์ํด ํ ์คํธ ๋ฌธ์์ ๋ฌธ์์ด์ ํ ํฐ์ผ๋ก ๋ถํดํ๊ณ , ํ ํฐ์ ๋ฌธ๋ฒ์ ์๋ฏธ์ ๊ตฌ์กฐ๋ฅผ ๋ฐ์ํ์ฌ ํธ๋ฆฌ ๊ตฌ์กฐ์ ์๋ฃ ๊ตฌ์กฐ์ธ ํ์ค ํธ๋ฆฌ(parse tree)๋ฅผ ์์ฑํ๋ ์ผ๋ จ์ ๊ณผ์
๐โโ๏ธ ๋ ธ๋(node)๋,
HTML ์์๋ ๋ ๋๋ง ์์ง์ ์ํด ํ์ฑ๋์ด DOM์ ๊ตฌ์ฑํ๋ ์์ ๋ ธ๋ ๊ฐ์ฒด๋ก ๋ณํ. ์ด๋ HTML ์์์ ์ดํธ๋ฆฌ๋ทฐํธ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋๋ก, HTML ์์์ ํ ์คํธ ์ฝํ ์ธ ๋ ํ ์คํธ ๋ ธ๋๋ก ๋ณํ.
Document.prototype.getElementById
์ธ์๋ก ์ ๋ฌํ id ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๊ฐ๋ ํ๋์ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ
Document.prototype.getElementsByTagName
Element.prototype.getElementsByTagName
์ธ์๋ก ์ ๋ฌํ ํ๊ทธ ์ด๋ฆ์ ๊ฐ๋ ๋ชจ๋ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ
๐ Ref.
Array.from()
HTMLCollection์ ๋ฐฐ์ด์ด ์๋๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉ X.
'Array.from()'๋ฉ์๋๋ฅผ ํตํด ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ฃผ๋ฉด ๋ฐฐ์ด ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
Document.prototype.getElementsByClassName
Element.prototype.getElementsByClassName
์ธ์๋ก ์ ๋ฌํ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๊ฐ๋ ๋ชจ๋ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ
- ์ธ์๋ก ์ ๋ฌํ class ๊ฐ์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ์ฌ๋ฌ ๊ฐ์ class์ง์ ๊ฐ๋ฅ
- ํด๋น class๋ฅผ ๊ฐ๋ ์์๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๋น HTMLCollection ๊ฐ์ฒด ๋ฐํ
Document.prototype.querySelector
Element.prototype.querySelector
์ธ์๋ก ์ ๋ฌํ CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ํ๋์ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ
- CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ์์ ๋ ธ๋๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ null ๋ฐํ
Document.prototype.querSelectorAll
Element.prototype.querSelectorAll
์ธ์๋ก ์ ๋ฌํ CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ๋ชจ๋ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ
- ์ฌ๋ฌ ๊ฐ์ ์์ ๋ ธ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ DOM ์ปฌ๋ ์ ๊ฐ์ฒด์ธ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐํ
- NodeList - ์ ์ฌ ๋ฐฐ์ด, ์ดํฐ๋ฌ๋ธ(์ํ ๊ฐ๋ฅ) ๐ forEach ๋ฉ์๋ ์ฌ์ฉ ๊ฐ๋ฅ
- CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ์์๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๋น NodeList ๊ฐ์ฒด ๋ฐํ
๐โโ๏ธ HTMLCollection๊ณผ NodeList๋,
DOM API๊ฐ ์ฌ๋ฌ ๊ฐ์ ๊ฒฐ๊ณผ ๊ฐ์ ๋ฐํํ๊ธฐ ์ํ DOM ์ปฌ๋ ์ ๊ฐ์ฒด
- ๋ ๋ค ์ ์ฌ ๊ฐ์ฒด ๋ฐฐ์ด ๋ฐ ์ดํฐ๋ฌ๋ธ(์ํ ๊ฐ๋ฅ)
- getElementsByClassName, getElementsByTagName ๋ฉ์๋๊ฐ ๋ฐํํ๋ ๊ฐ์ฒด
- ๋ ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณํ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ๋ ์ด์์๋(live) DOM ์ปฌ๋ ์ ๊ฐ์ฒด
- Live DOM์ ๊ฒฝ์ฐ for๋ฌธ ์ํ๋ก ๋ ธ๋ ๊ฐ์ฒด ์ํ ๋ณ๊ฒฝ ์ ์ฃผ์ ํ์ (์๋ํ ๋๋ก ์๋ X) ๐ ์๋์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ํํผ ๋ฐ ์ฌ์ฉ ๊ฐ๋ฅ
- ์ญ์์ผ๋ก ์ํ
- while๋ฌธ ์ฌ์ฉ
- HTMLCollection ๊ฐ์ฒด๊ฐ ์๋ ๋ฐฐ์ด๋ก ๋ณํ (๊ฐ์ฅ ๊ถ์ฅ)
- querySelectorAll ๋ฉ์๋๊ฐ ๋ฐํํ๋ ๊ฐ์ฒด
- ์ค์๊ฐ์ผ๋ก ๋ ธ๋ ๊ฐ์ฒด์ ์ํ๋ฅผ ๋ณ๊ฒฝํ์ง ์๊ธฐ(non-live) ๋๋ฌธ์ HTMLCollection์ ๋ถ์์ฉ์ ํด๊ฒฐ
NodeList.prototype.forEach
๋ฉ์๋๋ฅผ ์์๋ฐ์ ์ฌ์ฉ ๊ฐ๋ฅ
๐ Warning
๋๋ถ๋ถ์ NoeList๋ non-live๋ก ๋์ํ์ง๋งchildNodes
ํ๋กํผํฐ๊ฐ ๋ฐํํ๋ NodeList ๊ฐ์ฒด๋ live๊ฐ์ฒด๋ก ๋์ํ๋ฏ๋ก ์ฃผ์ ํ์
๐ Ref.
๊ณต๋ฐฑ ํ
์คํธ ๋
ธ๋
* HTML ์์ ์ฌ์ด์ ์คํ์ด์ค, ํญ, ์ค๋ฐ๊ฟ(๊ฐํ) ๋ฑ์ ๊ณต๋ฐฑ ๋ฌธ์๋ ํ
์คํธ ๋
ธ๋๋ฅผ ์์ฑ
Node.prototype.childNodes
์์ ๋ ธ๋(์์๋ ธ๋, ํ ์คํธ๋ ธ๋)๋ฅผ ํ์ํ์ฌ NodeList์ ๋ด์ ๋ฐํ
Node.prototype.firstChild
์ฒซ ๋ฒ์งธ ์์ ๋ ธ๋(์์๋ ธ๋, ํ ์คํธ๋ ธ๋) ๋ฐํ
Node.prototype.lastChild
๋ง์ง๋ง ์์ ๋ ธ๋(์์๋ ธ๋, ํ ์คํธ๋ ธ๋) ๋ฐํ
Element.prototype.children
์์ ๋ ธ๋ ์ค ์์ ๋ ธ๋๋ง ํ์ํ์ฌ HTMLCollection์ ๋ด์ ๋ฐํ
Element.prototype.firstElementChild
์ฒซ ๋ฒ์งธ ์์ ์์ ๋ ธ๋ ๋ฐํ
Element.prototype.lastElementChild
๋ ๋ฒ์งธ ์์ ์์ ๋ ธ๋ ๋ฐํ
Node.prototype.hasChildNodes
ํ ์คํธ ๋ ธ๋๋ฅผ ํฌํจํ์ฌ ์์ ๋ ธ๋์ ์กด์ฌ๋ฅผ ํ์ธ
Node.prototype.parentNode
- ํ ์คํธ ๋ ธ๋๋ DOM ํธ๋ฆฌ์ ์ต์ข ๋จ ๋ ธ๋์ธ ๋ฆฌํ ๋ ธํธ(leaf node)์ด๋ฏ๋ก ๋ถ๋ชจ๊ฐ ํ ์คํธ ๋ ธ๋์ธ ๊ฒฝ์ฐ๋ X
Node.prototype.previousSibling
ํ์ ๋ ธ๋ ์ค ์์ ์ ์ด์ ํ์ ๋ ธ๋(์์๋ ธ๋, ํ ์คํธ๋ ธ๋) ๋ฅผ ํ์ํ์ฌ ๋ฐํ
Node.prototype.nextSibling
ํ์ ๋ ธ๋ ์ค ์์ ์ ๋ค์ ํ์ ๋ ธ๋(์์๋ ธ๋, ํ ์คํธ๋ ธ๋) ๋ฅผ ํ์ํ์ฌ ๋ฐํ
Element.prototype.previousElementSibling
ํ์ ์์ ๋ ธ๋ ์ค ์์ ์ ์ด์ ํ์ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ
Element.prototype.nextElementSibling
ํ์ ์์ ๋ ธ๋ ์ค ์์ ์ ๋ค์ ํ์ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ
Node.prototype.nodeType
: ๋ ธ๋ ๊ฐ์ฒด ์ข ๋ฅ, ์ฆ ๋ ธ๋ ํ์ ์ ๋ํ๋ด๋ ์์ ๋ฐํ
Node.prototype.nodeName
: ๋ ธ๋์ ์ด๋ฆ์ ๋ฌธ์์ด๋ก ๋ฐํ
๐โโ๏ธ nodeValue๋,
setter์ getter๊ฐ ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ
Node.prototype.nodeValue
- nodeValue ํ๋กํผํฐ ์ฐธ์กฐ ์ ๋ ธ๋ ๊ฐ์ฒด์ ๊ฐ(ํ ์คํธ ๋ ธ๋์ ํ ์คํธ)์ ๋ฐํ
- ํ ์คํธ ๋ ธ๋๊ฐ ์๋ ๋ ธ๋(๋ฌธ์๋ ธ๋, ์์๋ ธ๋)์ ๊ฒฝ์ฐ null์ ๋ฐํ
- ํ ์คํธ ๋ ธ๋์ nodeValue ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ๋ฉด ํ ์คํธ ๋ ธ๋์ ๊ฐ ๋ณ๊ฒฝ ๊ฐ๋ฅ
๐โโ๏ธ textContent๋,
setter, getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ. ์์ ๋ ธ๋์ ํ ์คํธ์ ๋ชจ๋ ์์ ๋ ธ๋์ ํ ์คํธ๋ฅผ ์ทจ๋ํ๊ฑฐ๋ ๋ณ๊ฒฝ
Node.prototype.textContent
- ์์ ๋ ธ๋์ textContent ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด ์์ ๋ ธ๋์ ์ฝํ ์ธ ์์ญ ๋ด์ ํ ์คํธ๋ฅผ ๋ชจ๋ ๋ฐํ (์ด ๋ HTML ๋งํฌ์ ์ ๋ฌด์)
- ์์ ๋ ธ๋์ textContent ํ๋กํผํฐ์ ๋ฌธ์์ด์ ํ ๋นํ๋ฉด ์์ ๋ ธ๋์ ๋ชจ๋ ์์ ๋ ธ๋๊ฐ ์ ๊ฑฐ๋๊ณ ํ ๋นํ ๋ฌธ์์ด์ด ํ ์คํธ๋ก ์ถ๊ฐ (์ด ๋๋ HTML ๋งํฌ์ ์ ๋ฌด์๋๊ณ ๋ฌธ์์ด ํ ์คํธ๋ก ์ทจ๊ธ)
- NodeValue๋ณด๋ค ๋ ๊ฐํธ
Element.prototype.innerHTML
ํ๋กํผํฐ๋ setter์ getter๊ฐ ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก ์์ ๋ ธ๋์ HTML ๋งํฌ์ ์ ์ทจ๋ํ๊ฑฐ๋ ๋ณ๊ฒฝ
- ํ๊ทธ ์๋ฆฌ๋จผํธ์ ๊ฐ์ ์ฝ๊ฑฐ๋, ๋ณ๊ฒฝํ ๋
innerHTML
์์ฑ์ ์ฌ์ฉ- ๊ธฐ์กด์ ์์ ๋ ธ๋๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ๊ณ ์๋กญ๊ฒ ์์ ๋ ธ๋๋ฅผ ์์ฑํ์ฌ DOM์
๋ฐ์ํ๋ ๋นํจ์จ์ ๋์. (insertAdjacentHTML ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ํจ์จ์ )- ์ฅ์
- ๊ตฌํ์ด ๊ฐ๋จํ๊ณ ์ง๊ด์
- ๋จ์
- ์ฌ์ฉ์๋ก๋ถํฐ ์ ๋ ฅ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก innerHTML ํ๋กํผํฐ์ ํ ๋นํ๋ ๊ฒ์ XSS(Cross-site Scripting Attacks)์ ์ทจ์ฝํ๋ฏ๋ก ์ํ
Element.prototype.insertAdjacentHTML(position, DOMString)
๋ฉ์๋๋ ๊ธฐ์กด ์์๋ฅผ ์ ๊ฑฐํ์ง ์์ผ๋ฉด์ ์์น๋ฅผ ์ง์ ํด ์๋ก์ด ์์๋ฅผ ์ฝ์
- ์ฒซ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ์ ์๋ ๋ฌธ์์ด์
beforebegin
,afterbegin
,beforeend
,afterend
์ 4๊ฐ์งinsertAdjacentHTML
๋ฉ์๋๋ ๊ธฐ์กด ์์์ ์ํฅ์ ์ฃผ์ง ์๊ณ ์๋กญ๊ฒ ์ฝ์ ๋ ์์๋ง์ ํ์ฑํ์ฌ ์์ ์์๋ก ์ถ๊ฐ
Document.prototype.createElement(tagName)
: ์ธ์๋ก ์ ๋ฌ๋ฐ์ ํ๊ทธ ์ด๋ฆ์ ํด๋นํ๋ ์์ ๋ ธ๋๋ฅผ ์์ฑํ์ฌ ๋ฐํ
Document.prototype.createTextNode(text)
: ์ธ์๋ก ์ ๋ฌ ๋ฐ์ ํ ์คํธ ๊ฐ์ผ๋ก ํ ์คํธ ๋ ธ๋๋ฅผ ์์ฑํ์ฌ ๋ฐํ
Node.prototype.appendChild(childNode)
: ์ธ์๋ก ์ ๋ฌ ๋ฐ์ ๋ ธ๋๋ฅผ appendChild ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ธ๋์ ๋ง์ง๋ง ์์ ๋ ธ๋๋ก ์ถ๊ฐ
Node.prototype.appendChild(childNode)
: ์ธ์๋ก ์ ๋ฌ ๋ฐ์ ๋ ธ๋๋ฅผ appendChild ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ธ๋์ ๋ง์ง๋ง ์์ ๋ ธ๋๋ก ์ถ๊ฐ
Node.prototype.insertBefore(newNode, childNode)
: ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ ๋ฐ์ ๋ ธ๋๋ฅผ ๋ ๋ฒ์ฌ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ ธ๋ ์์ ์ฝ์ . ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ ๋ฐ์ ๋ ธ๋๋ ๋ฐ๋์ insertBefore๋ฅผ ํธ์ถํ ๋ ธ๋์ ์์ ๋ ธ๋์ฌ์ผํจ.
Node.prototype.cloneNode([deep: true | false])
๋ฉ์๋๋ ๋ ธ๋์ ์ฌ๋ณธ์ ์์ฑํ์ฌ ๋ฐํ
- ๋งค๊ฐ๋ณ์ deep์ true๋ฅผ ์ธ์๋ก ์ ๋ฌํ๋ฉด ๋ ธ๋๋ฅผ ๊น์ ๋ณต์ฌํ์ฌ ๋ชจ๋ ์์ ๋ ธ๋๊ฐ ํฌํจ๋ ์ฌ๋ณธ์ ์์ฑํ๊ณ , false๋ฅผ ์ธ์๋ก ์ ๋ฌํ๊ฑฐ๋ ์๋ตํ๋ฉด ๋ ธ๋๋ฅผ ์์ ๋ณต์ฌํ์ฌ ๋ ธ๋ ์์ ๋ง์ ์ฌ๋ณธ์ ์์ฑ
Node.prototype.replaceChild(newChild, oldChild)
๋ฉ์๋๋ ์์ ์ด ํธ์ถํ ๋ ธ๋์ ์์ ๋ ธ๋์ธ oldChild ๋ ธ๋๋ฅผ newChild ๋ ธ๋๋ก ๊ต์ฒด. ์ด๋ oldChild๋ replaceChild ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ธ๋์ ์์ ๋ ธ๋์ฌ์ผ ํ๊ณ oldChild๋ DOM์์ ์ ๊ฑฐ ๋จ.
Node.prototype.removeChild(child)
๋ฉ์๋๋ child ๋งค๊ฐ๋ณ์์ ์ธ์๋ก ์ ๋ฌํ ๋ ธ๋๋ฅผ DOM์์ ์ญ์ . ์ธ์๋ก ์ ๋ฌํ ๋ ธ๋๋ removeChild ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ธ๋์ ์์์ด์ด์ผํจ
Element.prototype.attribute
ํ๋กํผํฐ๋ก ๋ชจ๋ ์ดํํฐ๋ทฐํธ ๋ ธ๋๋ฅผ ์ทจ๋ ๊ฐ๋ฅ
- attributes ํ๋กํผํฐ๋ getter๋ง ์กด์ฌํ๋ ์ฝ๊ธฐ ์ ์ฉ ์ ๊ทผ์ ํ๋กํผํฐ๋ก NamedNodeMap ๊ฐ์ฒด๋ฅผ ๋ฐํ
Element.prototype.getAttribute/setAttribute
๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด attributesํ๋กํผํฐ๋ฅผ ํตํ์ง ์๊ณ ์์ ๋ ธ๋์์ ๋ฉ์๋๋ฅผ ํตํด ์ง์ HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋ํ๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์์ด ํธ๋ฆฌElement.prototype.hasAttribute(attributeName)
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ํน์ HTML ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์กด์ฌํ๋์ง ํ์ธ ๊ฐ๋ฅElement.prototype.removeAttribute(attributeName)
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ํน์ HTML ์ดํธ๋ฆฌ๋ทฐํธ ์ญ์ ๊ฐ๋ฅ
- HTML ์ดํธ๋ฆฌ๋ทฐํธ : HTML ์์์ ์ด๊ธฐ ์ํ๋ฅผ ์ง์ ํ๋ฉฐ ๋ณํ์ง ์์ .์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋์์ ๊ด๋ฆฌ๋๋ฉฐ ๊ฐ์ ์ป๊ฑฐ๋ ๋ณ๊ฒฝํ๋ ค๋ฉด
getAttribute/setAttribute
๋ฉ์๋ ์ฌ์ฉ- DOMํ๋กํผํฐ : ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ต์ ์ํ๋ฅผ ๊ด๋ฆฌ. ์ฌ์ฉ์์ ์ ๋ ฅ์ ์ํ ์ํ ๋ณํ์ ๋ฐ์ํ์ฌ ์ธ์ ๋ ์ต์ ์ํ๋ฅผ ์ ์ง.
data ์ดํธ๋ฆฌ๋ทฐํธ์ dataset ํ๋กํผํฐ๋ก HTML ์์์ ์ ์ํ ์ฌ์ฉ์ ์ ์ ์ดํธ๋ฆฌ๋ทฐํธ์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ ๋ฐ์ดํฐ ๊ตํ ๊ฐ๋ฅ.
- data ์ดํธ๋ฆฌ๋ทฐํธ๋ data- ์ ๋์ฌ ๋ค์์ ์์์ ์ด๋ฆ์ ๋ถ์ฌ ์ฌ์ฉ
HTMLElement.dataset
ํ๋กํผํฐ๋ก DOMStringMap ๊ฐ์ฒด๋ฅผ ๋ฐํ๋ฐ์ผ๋ฉด ์์์ ์ด๋ฆ์ ์นด๋ฉ ์ผ์ด์ค๋ก ๋ณํํ ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์์
HTMLElement.prototype.style
ํ๋กํผํฐ๋ setter์ getter๊ฐ ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก ์์ ๋ ธ๋์ ์ธ๋ผ์ธ ์คํ์ผ์ ์ทจ๋ํ๊ฑฐ๋ ์ถ๊ฐ ๋๋ ๋ณ๊ฒฝ.
CSSStyleDeclation ํ์ ์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฉฐ ์ด ํ๋กํผํฐ ๊ฐ์ ํ ๋นํ๋ฉด ํด๋น CSS ํ๋กํผํฐ๊ฐ ์ธ๋ผ์ธ ์คํ์ผ๋ก HTML ์์์ ์ถ๊ฐ๋๊ฑฐ๋ ๋ณ๊ฒฝ๋๋ค.
class ์ดํธ๋ฆฌ๋ทฐํธ์ ๋์ํ๋ DOM ํ๋กํผํฐ๋ class๊ฐ ์๋๋ผ className๊ณผ classList.
Element.prototype.className
ํ๋กํผํฐ๋ setter, getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก ์์ ๋ ธ๋์ className ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด class ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ๋ฌธ์์ด๋ก ๋ฐํํ๊ณ , ์์ ๋ ธ๋์ className ํ๋กํผํฐ์ ๋ฌธ์์ด์ ํ ๋นํ๋ฉด class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ํ ๋นํ ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ.
Element.prototype.classList
ํ๋กํผํฐ๋ class ์ดํธ๋ฆฌ๋ทฐํธ์ ์ ๋ณด๋ฅผ ๋ด์ DOMTokenList ๊ฐ์ฒด๋ฅผ ๋ฐํ.