The goals
๐ช๐ปHTML, DOM & JavaScript
โ๐ปNodes & Elements
๐๐ปQuerying DOM Nodes & Traversing the DOM
๐๐ปEvaluating & Manipulating DOM Nodes
๐๐ปCreating & Removing DOM Nodes
The Document Object Model (DOM)
Node Query Methods
๋ค์์ DOM ์์์ ์ ๊ทผํ๋ ๋ค์ํ ๋ฐฉ๋ฒ์ ๋ํ ์์ฝ์ ๋๋ค (์ฐธ๊ณ : ์์ ๋ ธ๋์ ๋ํด์๋ง ์ฟผ๋ฆฌํ ์ ์์).
์๋์ ์ฟผ๋ฆฌ ๋ฉ์๋ ์ธ์๋ ๋ฌธ์ ๊ฐ์ฒด์ ๋ํ ์๋์ ํน์ ํ๋กํผํฐ๋ก ๋ฌธ์์ ์ผ๋ถ๋ฅผ ์ ํํฉ๋๋ค.
document.body => <body> ์์ ๋ ธ๋๋ฅผ ์ ํํฉ๋๋ค.
document.head => <head> ์์ ๋ ธ๋๋ฅผ ์ ํํฉ๋๋ค.
document.documentElement => <html> ์์ ๋ ธ๋๋ฅผ ์ ํํฉ๋๋ค.
---
์ฟผ๋ฆฌ ๋ฉ์๋
---
document.querySelector(<CSS selector>);
๋ชจ๋ CSS ์ ํ์(์: '#id์ด๋ฆ', '.ํด๋์ค๋ช ' ๋๋ 'div p.ํด๋์ค๋ช ')๋ฅผ ์ฌ์ฉํ์ฌ DOM์์ ์ฒ์(!) ์ผ์นํ๋ ์์๋ฅผ ๋ฐํํฉ๋๋ค. ์ผ์นํ๋ ์์๋ฅผ ์ฐพ์ ์ ์๋ ๊ฒฝ์ฐ์๋ null ์ ๋ฐํํฉ๋๋ค.
์ถ๊ฐ ์ ๋ณด: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
document.getElementById(<ID>);
ID(# ์์ด ID ์ด๋ฆ๋ง)๋ฅผ ๊ฐ์ ธ์ ์ด ID๋ฅผ ๊ฐ์ง ์์๋ฅผ ๋ฐํํฉ๋๋ค. ๋์ผํ ID๊ฐ ํ์ด์ง์์ ๋ ๋ฒ ์ด์ ๋ฐ์ํ์ง ์์ผ๋ฏ๋ก ํญ์ ์ ํํ ํ๋์ ์์๋ฅผ ๋ฐํํฉ๋๋ค. ์ง์ ๋ ID๋ฅผ ๊ฐ์ง ์์๋ฅผ ์ฐพ์ ์ ์๋ ๊ฒฝ์ฐ null ์ ๋ฐํํฉ๋๋ค.
์ถ๊ฐ ์ ๋ณด: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
document.querySelectorAll(<CSS selector>);
๋ชจ๋ CSS ์ ํ์(์: '#id์ด๋ฆ', '.ํด๋์ค๋ช ' ๋๋ 'div p.ํด๋์ค๋ช ')๋ฅผ ์ฌ์ฉํ์ฌ DOM์์ ์ผ์นํ๋ ๋ชจ๋ ์์๋ฅผ ์ ์ (๋ ๋ผ์ด๋ธ) NodeList๋ก ๋ฐํํฉ๋๋ค. ์ผ์นํ๋ ์์๋ฅผ ์ฐพ์ ์ ์์ผ๋ฉด ๋น NodeList ๋ฅผ ๋ฐํํฉ๋๋ค.
์ถ๊ฐ ์ ๋ณด: https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
document.getElementsByClassName(<CSS CLASS>);
CSS ํด๋์ค g(์: 'ํด๋์ค๋ช ')๋ฅผ ๊ฐ์ ธ์ DOM์์ ์ผ์นํ๋ ์์์ ๋ผ์ด๋ธ HTMLCollection ์ ๋ฐํํฉ๋๋ค. ์ผ์นํ๋ ์์๋ฅผ ์ฐพ์ ์ ์์ผ๋ฉด ๋น HTMLCollection์ ๋ฐํํฉ๋๋ค.
์ถ๊ฐ ์ ๋ณด: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName
document.getElementsByTagName(<HTML TAG>);
HTML ํ๊ทธ(์: 'p')๋ฅผ ๊ฐ์ ธ์ DOM์์ ์ผ์นํ๋ ์์์ ๋ผ์ด๋ธ HTMLCollection์ ๋ฐํํฉ๋๋ค. ์ผ์นํ๋ ์์๋ฅผ ์ฐพ์ ์ ์์ผ๋ฉด ๋น HTMLCollection์ ๋ฐํํฉ๋๋ค.
์ถ๊ฐ ์ ๋ณด: https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName
์ผ๋ฐ์ ์ผ๋ก๋ ์ฌ์ฉ๋์ง ์๋ getElementsByName() ๋ฉ์๋๋ ์์ต๋๋ค(https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName<2 }).


Quiz
DOM์ ์ด๋์์ “์ฐพ์” ์ ์์๊น์?
๋ต: ๋ธ๋ผ์ฐ์ ์ ์ํด JavaScript์ ๋ ธ์ถ๋๋ API์ ์ผ๋ถ์ ๋๋ค.
JavaScript๋ "ํธ์คํ ๋ ์ธ์ด"๋ผ๋ ์ ์ ๊ธฐ์ตํ์ธ์. ํธ์คํธ ํ๊ฒฝ์ผ๋ก์์ ๋ธ๋ผ์ฐ์ ๋ ์ด DOM API๋ฅผ JS ์ฝ๋์ ์๋์ผ๋ก ๋ ธ์ถํฉ๋๋ค.
document.querySelector('#Id ์ด๋ฆ')์ document.getElementById('Id ์ด๋ฆ')์ ์ฐจ์ด์ ์ ๋ฌด์์ผ๊น์?
๋ต: querySelector๋ CSS ์ ํ์๋ก (์ ๊ณต๋ ์ ํ์์ ๋ฐ๋ผ) ๋ชจ๋ ์์์ ์ผ์นํ ์ ์์ผ๋ฉฐ getElementById๋ ID๋ก๋ง ์ฐพ์ต๋๋ค.
์ง๋ฌธ 4:
querySelector()์ querySelectorAll()์ ์ฐจ์ด์ ์ ๋ฌด์์ผ๊น์?
๋ต: querySelector๋ ์ฒ์ ์ผ์นํ๋ ์์๋ฅผ ์ฐพ๊ณ querySelectorAll์ ์ผ์นํ๋ ๋ชจ๋ ์์๋ฅผ ์ฐพ์ ๋ฐฐ์ด๊ณผ ์ ์ฌํ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.




์์ฝ: ์ฝ์ , ๋์ฒด, ์ ๊ฑฐ
DOM ์์๋ฅผ ์์ฑ, ์ฝ์ , ๊ต์ฒด, ์ ๊ฑฐํ๋ ๋ฐฉ๋ฒ์๋ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ต๋๋ค. ๋ค์์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฐฉ๋ฒ์ ๋ํ ์์ฝ์ ๋๋ค.
๋ธ๋ผ์ฐ์ ์ง์์ ๋ํด์๋ ์ ๊ณต๋ ๋งํฌ์ ๊ฐ์ ํ๋ฐ๋ถ์ ์๋ "๋ธ๋ผ์ฐ์ ์ง์" ๋ชจ๋์ ํ์ธํ์ญ์์ค.
์์ฑ & ์ฝ์
ํฌ๊ฒ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. HTML ์ค๋ํซ(์: innerHTML์ ํตํด)์ ์ ํจํ HTML ์ค๋ํซ์ ์ ๊ณตํ๊ณ ๋ธ๋ผ์ฐ์ ๊ฐ ์ด๋ฅผ ๋ ๋๋งํ๋๋ก ํ๊ฑฐ๋ JS ์ฝ๋๋ก DOM ๊ฐ์ฒด๋ฅผ ์์ฑํ ํ ์๋์ผ๋ก ์ถ๊ฐ/์ฝ์ ํฉ๋๋ค. ํ์์ ์ ๊ทผ ๋ฐฉ์์ DOM ๊ฐ์ฒด์ ์ง์ ์ ๊ทผํ ์ ์๋ค๋ ์ฅ์ ์ด ์์ต๋๋ค(ํ๋กํผํฐ๋ฅผ ์ค์ ํ๊ฑฐ๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ถ๊ฐํ๋ ๋ฐ ์ ์ฉ). ๋จ์ ์ ๋ ๋ง์ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํ๋ค๋ ๊ฒ๋๋ค.
HTML ์ฝ๋ ์ถ๊ฐํ๊ธฐ.
const root = document.getElementById('root-el'); // selects something like <div id="root-el">
root.innerHTML = `
<div>
<h2>Welcome!</h2>
<p>This is all create & rendered automatically!</p>
</div>
`;
์ค์ํ ์ฐธ๊ณ ์ฌํญ: innerHTML์ ์ฌ์ฉํ๋ฉด root์ ์๋ ๋ด์ฉ์ด ์์ ํ ๋์ฒด๋ฉ๋๋ค. HTML ์ฝ๋๋ฅผ ์ถ๊ฐ/์ฝ์ ํ๋ ค๋ฉด insertAdjacentHTML์ ๋์ ์ฌ์ฉํ ์ ์์ต๋๋ค. https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
const root = document.getElementById('root-el'); // selects something like <div id="root-el">
root.insertAdjacentHTML('afterbegin', `
<div>
<h2>Welcome!</h2>
<p>This is all create & rendered automatically!</p>
</div>
`);
DOM ๊ฐ์ฒด๋ฅผ ์๋์ผ๋ก ์์ฑ, ์ฝ์ ํ๊ธฐ.
const someParagraph = document.createElement('p'); // creates a "p" element (i.e. a <p> element)
const root = document.getElementById('root-el'); // selects something like <div id="root-el">
root.append(someParagraph);
์ด ์์์๋ ๋จ๋ฝ์ ๋ง๋ค์ด root์ ์ถ๊ฐํฉ๋๋ค - ์ด๋ root์ ๋์ ์ฝ์ ๋จ์ ์๋ฏธํฉ๋๋ค(์ฆ root ๋ด๋ถ์ ์์ง๋ง ๋ค๋ฅธ ๋ชจ๋ ํ์ ๋ ธ๋ ์ดํ์ ์ถ๊ฐ๋จ).
์ฝ์ ํ๋ ๋ฉ์๋:
append() => https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append
๋ธ๋ผ์ฐ์ ์ง์์ ๊ด์ฐฎ๊ฒ ๋๋ ํธ์ด์ง๋ง IE์์๋ appendChild()์ด ์ ํธ๋ ์ ์์ต๋๋ค => https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild
prepend() => https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/prepend
๋ธ๋ผ์ฐ์ ์ง์์ ๊ด์ฐฎ๊ฒ ๋๋ ํธ์ด์ง๋ง IE์์๋ insertBefore()์ด ์ ํธ๋ ์ ์์ต๋๋ค => https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore
before(), after() => https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/before & https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/after
๋ธ๋ผ์ฐ์ ๋ฅผ ์ง์ํ์ง๋ง IE์ Safari๋ ์ง์ํ์ง ์์ต๋๋ค. insertBefore() (https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore) or insertAdjacentElement() (https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement) as substitutes.
์ค์ํ ์ฐธ๊ณ ์ฌํญ(์์๋ฅผ ์ฝ์ ํ๋ ๋ฐฉ๋ฒ์ ๊ด๊ณ์์ด). ์์๋ฅผ ์ฝ์ ํ ๋ ์ด์ ์ ์ด๋ฏธ ์ฝ์ ํ ๊ฒฝ์ฐ ์ ์์น๋ก ์์๋ฅผ ์ด๋ํ๊ฒ ๋ฉ๋๋ค. ๋ณต์ฌ๋๋ ๊ฒ์ด์๋๋๋ค(someElement.cloneNode(true)๋ก ์์๋ฅผ ๋ณต์ฌํ ์ ์๊ธด ํฉ๋๋ค).
๋์ฒด
DOM์ ์์๋ฅผ ์๋์ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๊ต์ฒดํ ์ ์์ต๋๋ค.
replaceWith() => https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/replaceWith
replaceChild() => https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild
replaceWith()๋ ์กฐ๊ธ ๋ ์ฝ๊ฒ ์ธ ์ ์๊ณ ๋ธ๋ผ์ฐ์ ์ง์๋ ์ ์ ํฉ๋๋ค. ๋จ, IE๋ ์์ธ์ ๋๋ค. IE๋ฅผ ์ฌ์ฉํ์ ๋ค๋ฉด replaceChild()๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ณ ๋ คํด ๋ณด์ธ์.
์ ๊ฑฐ
์๋์ ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์์๋ฅผ ์ ๊ฑฐํ ์ ์์ต๋๋ค:
someElement.innerHTML = ''=> someElement์ ๋ชจ๋ HTML ๋ด์ฉ์ ์ง์ ๋ ๋๋ง๋ ๋ชจ๋ ๊ฐ์ฒด๋ฅผ ์ ๊ฑฐํฉ๋๋ค.
someElement.remove()=> DOM์์ ๋จ์ผ ์์(someElement)๋ฅผ ์ ๊ฑฐ(https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove).
๋ธ๋ผ์ฐ์ ์ง์์ ํ๋ฅญํ์ง๋ง IE๋ ์๋๋๋ค. ๋์ removeChild(๋ค์์ ์ฐธ๊ณ )๋ฅผ ์ฌ์ฉํ์ธ์.
someElement.parentNode.removeChild(someElement) => (ํธ์ถํ๋ ์์๊ฐ ์๋) ์ ๊ณต๋ ์์ ์์๋ฅผ ์ ๊ฑฐํฉ๋๋ค. ๊ด๋ฒ์ํ ๋ธ๋ผ์ฐ์ ์ง์์ ์ ๊ณตํ์ง๋ง ๋ ๋ง์ ์ฝ๋๊ฐ ์์ฑ๋์ด์ผ ํฉ๋๋ค(https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild).
ํ ์คํธ ๋ ธ๋๋ ์ด๋จ๊น์?
ํ ๋ฒ์ ํ ์คํธ ๋ ธ๋๋ฅผ ์์ฑํ๊ณ ์ฝ์ ํ ์ ์์ต๋๋ค.
someElement.textContent = 'Hi there!';
์์ ์ฝ๋๋ 'Hi there!' ๋ด์ฉ์ ํ ์คํธ ๋ ธ๋๋ฅผ ์์ฑํ๊ณ ์ฝ์ ํฉ๋๋ค.
๊ธฐ์กด์ ์๋ ํ ์คํธ๋ฅผ ์ถ๊ฐํ๊ณ ์ถ์ผ์ ๊ฐ์?
์๋์ ์ฝ๋๋ฅผ ์ฐ๋ฉด ๋ฉ๋๋ค.
someElement.textContent = someElement.textContent + 'More text!';
Sources
Element.insertAdjacentHTML() - Web APIs | MDN
The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position.
developer.mozilla.org
Element.insertAdjacentHTML() - Web APIs | MDN
The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position.
developer.mozilla.org
Element.insertAdjacentHTML() - Web APIs | MDN
The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position.
developer.mozilla.org
Document.getElementById() - Web APIs | MDN
The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quick
developer.mozilla.org
Document.getElementById() - Web APIs | MDN
The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quick
developer.mozilla.org
Document.getElementById() - Web APIs | MDN
The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quick
developer.mozilla.org
Document.querySelector() - Web APIs | MDN
The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.
developer.mozilla.org
'JavaScript - The Complete Guide 2022' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Section 9: Objects - A Closer Look - Beyond the Basics (0) | 2022.10.19 |
---|---|
Section 8: Arrays & Iterables - Lists of Data (0) | 2022.10.18 |
Section 6: More about Functions - Beyond function basic() {} (0) | 2022.10.14 |
Section 5: Behind The Scenes of JavaScript - How It Works, The Weird Parts & ES5 vs ES6+ (0) | 2022.10.14 |
Section 4: Control Structures - Conditional Code & Loops (0) | 2022.10.13 |