DOM์ ๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ(Document Object Model)์ ์กฐ์ํ๋ ๋ฐ ์ฌ์ฉ๋๋ JavaScript์ API(์์ฉ ํ๋ก๊ทธ๋จ ํ๋ก๊ทธ๋๋ฐ ์ธํฐํ์ด์ค)์ ๋๋ค. DOM์ HTML, XML ๋๋ ๋ค๋ฅธ ๋ฌธ์ ์ ํ์ ๊ตฌ์กฐํ๋ ํํ์ ์์ฑํ๊ณ ํด๋น ๋ฌธ์์ ๊ตฌ์ฑ ์์๋ฅผ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ผ๋ก ์กฐ์ํ ์ ์๊ฒ ํด์ฃผ๋ ํ์ค ์ธํฐํ์ด์ค์ ๋๋ค.
๋๋ฌด ์ด๋ ค์ด ๋ง์ด์ง๋ง, ๋ป ๋ณด๋ค๋ DOM์ ์ฌ์ฉํด๋ณด๋ฉด ์น์ํด์ง๊ฒ๊ฐ๋ค.
๐ ์์ ๊ฒ์ํ๊ธฐ
๐ ์ฃผ์ node ํ๋กํผํฐ
๐ ์์ฑ๊ณผ ํ๋กํผํฐ
๐ ๋ฌธ์ ์์ ํ๊ธฐ
๐ ์คํ์ผ๊ณผ ํด๋์ค
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋ ์์๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค. ์ ํ์๋ CSS ์ ํ์์ ์ ์ฌํ๊ฒ ์๋ํ๋ฉฐ, ์ํ๋ ๋์์ ์ ํํ๊ฒ ์ง์ ํ ์ ์์ต๋๋ค.
์์ ์์ ์์ฃผ ์ฌ์ฉํ๋ ๋ฉ์๋๋ค์ ์ ๋ฆฌํด๋ณผ๊ฒ์ ๋๋ค.
์ ๋ฆฌํ๊ธฐ์ ์ ์์๋ฌ์ผํ ๋จ์ด๊ฐ ์์ต๋๋ค.
document : ์น ํ์ด์ง๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด๋ก์, ์น ํ์ด์ง์ ์ ์ฒด ๊ตฌ์กฐ์ ์ฝํ ์ธ ์ ๋ํ ํ๋ก๊ทธ๋๋ฐ์ ์ธ ์ ์ด๋ฅผ ์ ๊ณตํ๋ DOM์ ์ต์์ ๊ฐ์ฒด
๊ฐ๋จํ๊ฒ html์ ์ฒด๋ผ๊ณ ์๊ฐํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
const first = document.querySelector('.first');
const first = document.querySelector('#first');
//querySelector : ์ฃผ์ด์ง CSS ์ ํ์์ ํด๋นํ๋ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฐํํฉ๋๋ค.
const first = document.querySelectorAll('.first');
const first = document.querySelectorAll('#first');
//querySelectorAll: ์ฃผ์ด์ง CSS ์ ํ์์ ํด๋นํ๋ ๋ชจ๋ ์์๋ฅผ NodeList๋ก ๋ฐํํฉ๋๋ค.
๋ง์ด ์ฌ์ฉํ์ง ์์ง๋ง ์์๋ฌ์ผํ ๋ฉ์๋๋ ์๋์ ๊ฐ๋ค.
const second = document.getElementById('second')
//getElementById : ์ฃผ์ด์ง ID์ ํด๋นํ๋ ์์ ๋ฐํ
const second = document.getElementsByClassName('second')
//getElementsByClassName : ์ฃผ์ด์ง ํด๋์ค ์ด๋ฆ์ ํด๋นํ๋ ๋ชจ๋ ์์๋ฅผ ๋ฐํํฉ๋๋ค.
const second = document.getElementsByTagName('second')
//getElementsByTagName: ์ฃผ์ด์ง ํ๊ทธ ์ด๋ฆ์ ํด๋นํ๋ ๋ชจ๋ ์์๋ฅผ ๋ฐํํฉ๋๋ค.
๊ทธ ์ธ ๋ฌธ์์ ๋์์ ํ์ธํ๋ ๋ฉ์๋๋ ์๋ค.
//target(first)์ selector(span)๊ฐ ์์ด?
first.matches('span');
// ์ฃผ์ด์ง CSS ์ ํ์์ ์ผ์นํ๋์ง ์ฌ๋ถ
// h1 ํ๊ทธ์์ .second ๋ผ๋ ์์์ด ์์ด?
getNode('h1').contains(getNode('second'));
// ์ฃผ์ด์ง ์์๊ฐ ๋ค๋ฅธ ์์์ ์์์ธ์ง ์ฌ๋ถ
first.closest('.second)
//๋์ ๊ฐ์ฅ ์ธ์ ํ ์กฐ์์ ์ฐพ์์ค,๋ ํฌํจ.(ํ์ ๋ ์ฐพ์ ์ ์๋ค๊ณ ํจ.)
์์์์ getNode๋ querySelector๋ก ๋ง๋ ์์๋ฅผ ๋ฐํํ๋ ํจ์์ด๋ค.(์์ ์์ ๋ง๋ฌ.)
DOM์์ ์์๋ฅผ ์กฐ์ํ๊ณ ์ ๋ณด๋ฅผ ์ป๋ ๋ฐ ์ฌ์ฉ๋๋ ์์ฑ๋ค์ ์๋ฏธํฉ๋๋ค.
๐ Node๋ html์์ ๊ฐ์ฅ ์์ ์์๋ฅผ Node๋ผ๊ณ ๋ถ๋ฅธ๋ค๊ณ ํ๋ ์์๋์.
์ฌ์ค ๋ง๋ก ์ค๋ช ํ๊ธฐ ์ด๋ ค์์ ์ด๊ฒ ๋ํ ์ฌ์ฉํด๋ณด๋ฉด์ ์ต์ํด์ ธ์ผํ ๊ฒ๊ฐใท..ใ ..
๊ฐ์ฌ๋๊ป์ Node Type์ ์๊ณ ์์ผ๋ฉด ๋์์ง ์๋ค ๊ทธ๋์
๐ MDN ๋งํฌ Node Type
๋ฌธ์๋ฅผ ํ์ธํด๋ณด๋๊ฒ๋ ์ข์๊ฒ ๊ฐ๋ค. ํ์ง๋ง ๋ฌธ์๋ฅผ ๋ณด๊ธฐ ๊ท์ฐฎ๋ค๋ฉด ์๋์ ๊ฐ์ด node type์ ํ์ธ ํ ์ ์๋ค.
const first = getNode('.first');
//์ฐธ๊ณ ๋ก first๋ h1ํ๊ทธ์์ ์๋ span์ด๋ค.
console.log(first.nodeType); // 1
node์ ์ด๋ฆ์ ๋ํ๋ด๋ ์์ฑ๋ ์๋๋ฐ์
console.log(first.nodeName); //SPAN
console.log(first.tagName) //SPAN
nodeName tagName ์ฐจ์ด
<body><!-- ์ฃผ์ -->
<script>
// ์ฃผ์ ๋
ธ๋๋ฅผ ๋์์ผ๋ก ๋ ํ๋กํผํฐ ๋น๊ต
alert( document.body.firstChild.tagName ); // undefined (์์๊ฐ ์๋)
alert( document.body.firstChild.nodeName ); // #comment
// ๋ฌธ์ ๋
ธ๋๋ฅผ ๋์์ผ๋ก ๋ ํ๋กํผํฐ ๋น๊ต
alert( document.tagName ); // undefined (์์๊ฐ ์๋)
alert( document.nodeName ); // #document
</script>
</body>
์ด๋ ๊ฒ ๋์์์ง๋ง ์ค์ ๋ก tagName์ด ๋ง์ด ์ฐ์ธ๋ค๊ณ ํ๋ ์ฐธ๊ณ ํด์ผํ ๊ฒ๊ฐ๋ค.
๊ทธ๋ฆฌ๊ณ ๋ง์ฝ ์์๋ฅผ ๋น๊ตํ ๊ฒฝ์ฐ toLowerCase๋ฅผ ์ฐ์ง ์์ผ๋ฉด ๋น๊ต ๋์์ด ๋ฌด์กฐ๊ฑด ๋๋ฌธ์๋ก ์จ์ผ ๋น๊ต๊ฐ ๋๋ ์์๋ฌ์ผ๊ฒ ๋ค.
node์ ์ฝํ ์ธ ์ฝ๊ณ ์ฐ๊ธฐ
// - innerHTML
// * ๊ธฐ์กด ๋ด์ฉ ์ญ์
// * ๊ธฐ์กด ๋ด์ฉ๊ณผ ์๋ก์ด ๋ด์ฉ์ ํฉ์น ์๋ก์ด ๋ด์ฉ์ ์
// first.innerHTML = ''
//์
๋ ฅํ๋ฉด first ๋ด์ฉ ์ญ์ ๋จ. ์ฃผ์!!
first.innerHTML += '<strong>๋ฉ์์ด</strong> ์ฌ์์ฒ๋ผ';
// - textContent
// * ์์ ๋ด์ ํ
์คํธ์ ์ ๊ทผ
// * ํ๊ทธ๋ ์ ์ธํ๊ณ ์ค๋ก์ง ํ
์คํธ๋ง ์ถ์ถ
first.textContent = '๋ฉ์์ดํธ๋์ด์ฒ๋ผ'; // setter
first.textContent; // getter
์์ ๋ฐฉ๋ฒ์ ์ํํ ๋ฐฉ๋ฒ์ด๊ธฐ ๋๋ฌธ์ ์๋ ๋ฐฉ๋ฒ์ ๋ ์ถ์ฒ๋๋ฆฝ๋๋ค.
textContent๋ก ํ๊ทธ ์์ ๋ค์ด๊ฐ๋ ์์ํ ํ ์คํธ๋ง ๊ฐ์ ธ์ฌ ์ ์๋ค
<div id="news">
<h1>์ฃผ์ ๋ด์ค!</h1>
<p>ํ์ฑ์ธ์ด ์ง๊ตฌ๋ฅผ ์นจ๊ณตํ์์ต๋๋ค!</p>
</div>
alert(news.textContent); // ์ฃผ์ ๋ด์ค! ํ์ฑ์ธ์ด ์ง๊ตฌ๋ฅผ ์นจ๊ณตํ์์ต๋๋ค!
๋งค์ฐ ์ค์ํ ๋ด์ฉ์ธ๋ฐ์ ํ๋ก ํธ์๋๊ฐ๋ฐ์๋ค ๋ฉด์ ์ง๋ฌธ์ค์ ๋ง์ด ๋์จ๋ค๊ณ ํ๋ ์ธ์๋ฌ์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค.
HTML ์์ฑ
๋ธ๋ผ์ฐ์ ๋ HTML ํ๊ทธ๋ฅผ ํด์ํด DOM ๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋ HTML ํ์ค ์์ฑ์ ์ธ์ํ๊ณ ,
์ด ํ์ค ์์ฑ์ ์ฌ์ฉํด DOM ํ๋กํผํฐ๋ฅผ ์์ฑํฉ๋๋ค. ํ์ค ์์ฑ์ด ์๋ ๊ฒฝ์ฐ,
์ด์ ๋งคํํ๋ DOM ํ๋กํผํฐ๊ฐ ์์ฑ๋์ง ์์ต๋๋ค.
HTML ์์ฑ ๊ฐ์ ํญ์ ๋ฌธ์์ด์
๋๋ค.
DOM ํ๋กํผํฐ
DOM ๋
ธ๋(DOM node)๋ JavaScript ๊ฐ์ฒด์
๋๋ค. DOM ํ๋กํผํฐ์ ๋ฉ์๋๋ ์ผ๋ฐ JavaScript ๊ฐ์ฒด์ฒ๋ผ ํ๋ํ๋ฏ๋ก ์๋์ ๊ฐ์ ํน์ง์ ๋ณด์
๋๋ค.
์ด๋ค ๊ฐ์ด๋ ๊ฐ์ง ์ ์์ต๋๋ค.
๋ยท์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ฏ๋ก elem.nodeType
์ด ์๋, elem.NoDeTyPe
๋ ๋์ํ์ง ์์ต๋๋ค.
DOM ํ๋กํผํฐ๋ HTML ์์ฑ๊ณผ ๋ฌ๋ฆฌ ๊ฐ์ด ํญ์ ๋ฌธ์์ด์ด ์๋๋๋ค.
์์ฑ๊ณผ ํจ๊ป ์ฐ์ด๋ ๋ฉ์๋ ์๋์ ๊ฐ์ต๋๋ค.
/*first์ html๋ด์ฉ*/
<span class="first" say="hi">hello</span>
const first = getNode('.first');
// - elementNode.hasAttribute(name) โ ์์ฑ ์กด์ฌ ์ฌ๋ถ ํ์ธ
console.log(first.hasAttribute('id')); // false
// - elementNode.getAttribute(name) โ ์์ฑ๊ฐ์ ๊ฐ์ ธ์ด
console.log(first.getAttribute('say')); //hi
// - elementNode.setAttribute(name, value) โ ์์ฑ๊ฐ์ ๋ณ๊ฒฝํจ
first.setAttribute('id', 'text');
first.setAttribute('data-index', '1');
//<span class="first" say="hi" id="text" data-index="1" data-name="tiger">hello</span> ๋ณ๊ฒฝ
// - elementNode.removeAttribute(name) โ ์์ฑ๊ฐ์ ์ง์
first.removeAttribute('class'); //class์์ฑ์ด ์ฌ๋ผ์ง
// - elementNode.attributes โ ์ด๊ฑฐ ๊ฐ๋ฅํ(iterable) ์์ฑ ์งํฉ์ ๋ฐํํจ
for (const value of first.attributes) {
console.log(value);
} // say id data-index
๋นํ์ค ์์ฑ, ํ๋กํผํฐ ์ค์
๐ id๋ class๊ฐ์ ํ์ค์์ฑ์ด ์๋ ๋นํ์ค์์ฑ๋ค์ ๊ด๋ก์ ์ผ๋กdata๋ฅผ ์ ์ด์ฃผ๊ฒ๋์ด์์.
first.dataset.name = 'tiger';
//setter
console.log(first.dataset);// console.log์ ๋์ค๋ ๋ด์ฉ์ ์๋ ์ฌ์ง์ผ๋ก ๋ณด์ฌ๋๋ฆด๊ฒ์
//getter
์ฌ๊ธฐ์ ๊ฐ์ฅ ์ค์ํ๊ฒ ์์์ผํ๋ ๋ฉ์๋๋ ์๋์ ๊ฐ์ต๋๋ค.
์์น๋ง ์ ๊ธฐ์ตํด์ฃผ๋ฉด ์ฐ๋๋ฐ ์ด๋ ค์ธ๊ฒ ๊ฐ์ง ์์ต๋๋ค.
insertAdjacentHTML : ์ง์ ๋ ์์น(position)์ HTML ๋ฌธ์์ด(text)์ ์ฝ์ ํฉ๋๋ค.
// - className โ ํด๋์ค ์ ์ฒด๋ฅผ ๋ฌธ์์ด ํํ๋ก ๋ฐํํด์ฃผ๋ ํ๋กํผํฐ๋ก ํด๋์ค ์ ์ฒด๋ฅผ ๊ด๋ฆฌํ ๋ ์ ์ฉ
console.log(first.className); //getter first
//first.className = 'fff' //setter
//first.className = '' //์ ์ฒด์ญ์
์๋์ ํด๋์ค๋ ์์ ์ ์์ฃผ ๋์์ ์๊ณ ์๋๊ฒ ์ข์๊ฒ๊ฐ์ต๋๋ค.ใ ใ
// - classList โ ํด๋์ค ํ๋๋ฅผ ๊ด๋ฆฌํ ์ ์๊ฒ ํด์ฃผ๋ ๋ฉ์๋๋ก ๊ฐ๋ณ ํด๋์ค๋ฅผ ์กฐ์ํ ๋ ์ ์ฉ
first.classList.add('hello'); // hello ํด๋์ค ๋ํ๊ธฐ
first.classList.remove('hello'); // hello ํด๋์ค ์ง์ฐ๊ธฐ
console.log(first.classList.contains('hello'));
//contains : ํด๋์ค๊ฐ ์๋์ง ์๋์ง
// - style.cssText - "style" ์์ฑ ์ ์ฒด์ ๋์ํ๋ฏ๋ก ์คํ์ผ ์ ์ฒด์ ๋ํ ๋ฌธ์์ด ์ ์ฅ
first.style.cssText = `
display:flex;
margin:10px;
border:1px dotted red;
`;
first.style.background = 'orange'; //setter
console.log(getComputedStyle(first)['backgroundColor']); //getter
//getComputedStyle(element, [pseudoElement]) `์ฝ๊ธฐ ์ ์ฉ`
๐์ฐธ๊ณ ์ฌ์ดํธ
๐chatGPT
๐Javascript info
๐ํ ๋ธ๋ก๊ทธ ๊ธ..
๐๋ฉ์ฌ ์์
๋ด์ฉ...