DOM ์ HTML ๋ฌธ์์ ๋ด์ฉ์ ํธ๋ฆฌํํ๋ก ๊ตฌ์กฐํํ์ฌ ์นํ์ด์ง์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ฅผ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ์ญํ ์ ํฉ๋๋ค. ์ด๋ ๊ฐ๊ฐ์ ์์์ ์์ฑ, ์ฝํ
์ธ ๋ฅผ ํํํ๋ ๋จ์๋ฅผ '๋
ธ๋(node)'๋ผ๊ณ ํฉ๋๋ค.
์๋ ์ฝ๋๋ฅผ ํตํด ํธ๋ฆฌ๋ฅผ ํํํด ๋ด ์๋ค.
document.head
document.body
document.body.childNodes
document.body.childNodes[1]
document.body.childNodes[1].tagName
document.body.childNodes[1].innerText
document.body.childNodes[1]. //์ ๋ง ์ฐ์ด์ ์ผ๋ง๋ ๋ง์ ์ ํธ๋ฆฌ๋ทฐํธ๊ฐ ์๋์ง ํ์ธํด๋ณด์ธ์.
document.body.childNodes[2]
document.body.childNodes[2].data
๐ง API๋ย **Application Programming Interface** ์
๋๋ค. ์ฝ๊ฒ โ์ค๋ช
์โ๋ผ๊ณ ๊ธฐ์ตํด์ฃผ์๋ฉด ๋ฉ๋๋ค.
document ๊ฐ์ฒด๋ฅผ ํตํด HTML ๋ฌธ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํฉ๋๋ค. document๋ ๋ธ๋ผ์ฐ์ ๊ฐ ๋ถ๋ฌ์จ ์นํ์ด์ง๋ฅผ ๋ํ๋ด๋ฉฐ, DOM ํธ๋ฆฌ์ ์ง์ ์ ์ญํ ์ ์ํํฉ๋๋ค.
// ํด๋นํ๋ Id๋ฅผ ๊ฐ์ง ์์์ ์ ๊ทผํ๊ธฐ
document.getElementById();
// ํด๋นํ๋ ๋ชจ๋ ์์์ ์ ๊ทผํ๊ธฐ
document.getElementsByTagName();
// ํด๋นํ๋ ํด๋์ค๋ฅผ ๊ฐ์ง ๋ชจ๋ ์์์ ์ ๊ทผํ๊ธฐ
document.getElementsByClassName();
// โญcss ์ ํ์๋ก ๋จ์ผ ์์์ ์ ๊ทผํ๊ธฐ, ๊ฐ์ฅ ๋จผ์ ๋์ค๋ ์์ 1๊ฐ
document.querySelector("selector");
// โญcss ์ ํ์๋ก ์ฌ๋ฌ ์์์ ์ ๊ทผํ๊ธฐ
document.querySelectorAll("selector");
๐ค **HTMLCollection๊ณผ NodeList**
getElementsByโฆ ๋ฉ์๋์ querySelectorAll ๋ฉ์๋๋ ๋ชจ๋ ์กฐ๊ฑด์ ์ผ์นํ๋ ๋ชจ๋ ์์๋ฅผ ์ฐพ๋๋ค๋ ๊ณตํต์ ์ธ ๊ธฐ๋ฅ์ ์ํํฉ๋๋ค. ํ์ง๋ง ๊ทธ ๊ฒฐ๊ณผ๋ฌผ์ ์ฝ์์ฐฝ์์ ํ์ธํ๋ฉด ์กฐ๊ธ ๋ค๋ฅด๋ค๋๊ฒ์ ์๊ฒ ๋ฉ๋๋ค.
<article id="container">
<ul>
<li>ํ์์ก</li>
<li class="item-second">์ ์ฐ์ฌ</li>
<li>์ง์ฅ๋ฉด</li>
</ul>
</article>
<script>
const cont = document.getElementById('container');
const item1 = cont.getElementsByTagName('li');
const item3 = cont.querySelectorAll('li');
</script>
์์ ๊ฐ์ ์ฝ๋๋ฅผ ์์ฑํ์์ ๋ item1๊ณผ item3์ ์ฝ์์ ์ฐ์ด๋ณด๋ฉด ๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ์ต๋๋ค.
HTMLCollection๊ณผ NodeList ๋ ๋ชจ๋ ๋ฐฐ์ด๊ณผ ๋น์ทํ ๊ฐ์ฒด(array-like object) ํํ๋ฅผ ๊ฐ์ง๋๋ค. ๋๋ฌธ์ ์์ ์๋ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํด์ ๋ฐฐ์ด์ฒ๋ผ ์์์ ์ธ๋ฑ์ค๋ก ์ ๊ทผํ๋ค๋ ๊ณตํต์ ์ด ์์ง๋ง,
๊ฐ์ฅ ํฐ ์ฐจ์ด์ ์ ํด๋น ๊ฐ์ฒด์ ํฌํจ๋ ์ ์๋ DOM ์์์ ์ ํ์ ๋๋ค. HTMLCollection์ HTML๋ง ํฌํจํ์ง๋ง, NodeList๋ ๋ชจ๋ ์ ํ์ DOM ์์ ์ฆ, text, ์ฃผ์ ๋ฑ์ ๋ชจ๋ ํฌํจํฉ๋๋ค.
๋ ๋ค๋ฅธ ์ฐจ์ด์ ์ ๊ฐ์ฒด๋ฅผ ๊ตฌ์ฑํ๋ ๊ฐ์ด ๋ณ๊ฒฝ ๊ฐ๋ฅํ์ง ์ฌ๋ถ์ ๋๋ค. HTMLCollection์ ์ค์๊ฐ์ผ๋ก ์ ๋ฐ์ดํธ๋๋ฉฐ, ํด๋น ๊ฐ์ฒด์ ๊ฐ ์์ฑ์ ๋ํ ๋ณ๊ฒฝ ์ฌํญ์ด ์ฆ์ ๋ฐ์๋ฉ๋๋ค. ๋ฐ๋ฉด์, NodeList๋ ์ ์ ์ด๋ฏ๋ก ํด๋น ๊ฐ์ฒด์ ๋ํ ๋ณ๊ฒฝ ์ฌํญ์ ์ฆ์ ๋ฐ์๋์ง ์์ต๋๋ค.
๋ง์ง๋ง์ผ๋ก ์ฌ์ฉํ ์ ์๋ ๋ฉ์๋๊ฐ ๋ค๋ฆ ๋๋ค. NodeList๋ forEach ๊ฐ์ ๋ฐฐ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ง๋ง HTMLCollection์ ๋ ๋ค๋ฅธ ๊ธฐ๋ฅ์ ์ง์ํฉ๋๋ค.
target.addEventListener( type, listener )
์ ๋ฌธ๋ฒ ํํ๋ฅผ ์ง๋๋๋ค.
<button>HELLO!</button>
// ์ด๋ฒคํธ์ ํ์
์๋ click, mouseover, mouseout, wheel ๋ฑ ๋ค์ํ ์ด๋ฒคํธ๋ฅผ ๊ฐ์งํฉ๋๋ค.
// listener ํจ์์ ์ธ์์๋ ์ด๋ฒคํธ์ ๋ํ ์ ๋ณด๊ฐ ๋ด๊ฒจ์์ต๋๋ค.
const myBtn = document.querySelector("button");
myBtn.addEventListener('click', function(){
console.log("hello world");
})
classList
๊ฐ์ฒด๋ฅผ ํตํด ์์์ class ์์ฑ์ ์ ์ดํ ์ ์์ต๋๋ค.
<button>Make me BLUE!</button>
const myBtn = document.querySelector("button");
myBtn.addEventListener('click', function(){
// blue ๋ผ๋ ํด๋์ค์ ์์ฑ ๊ฐ์ ์ง์ ํ ์ ์์ต๋๋ค.
myBtn.classList.add("blue");
// myBtn.classList.remove("blue"); ํด๋์ค๋ฅผ ์ ๊ฑฐํฉ๋๋ค.
// myBtn.classList.toggle("blue"); ํด๋์ค๋ฅผ ํ ๊ธํฉ๋๋ค. ์์ผ๋ฉด ๋ฃ์ด์ฃผ๊ณ , ์์ผ๋ฉด ์ ๊ฑฐํฉ๋๋ค.
// myBtn.classList.contains("blue"); ํด๋นํ๋ ํด๋์ค๊ฐ ์๋์ง ํ์ธํฉ๋๋ค.
})
DOM api๋ฅผ ์ด์ฉํ๋ฉด ์์๋ฅผ ์๋กญ๊ฒ ์์ฑํ๊ณ , ์์นํ๊ณ , ์ ๊ฑฐํ ์ ์์ต๋๋ค.
document.createElement(target)
: target ์์๋ฅผ ์์ฑํฉ๋๋ค.document.createTextNode(target)
: target ํ
์คํธ๋ฅผ ์์ฑํฉ๋๋ค.element.appendChild(target)
: target ์์๋ฅผ element์ ์์์ผ๋ก ์์นํฉ๋๋ค.element.removeChild(target)
: element์ target ์์ ์์๋ฅผ ์ ๊ฑฐํฉ๋๋ค.<ul></ul>
<button>Make me MORE!</button>
const myBtn = document.querySelector("button");
const myUl = document.querySelector("ul");
myBtn.addEventListener('click', function(){
for(let i=0; i < 5; i++){
const myLi = document.createElement('li');
myUl.appendChild(myLi);
}
})
element.append(target)
: target ์์๋ฅผ element์ ์์์ผ๋ก ์์นํฉ๋๋ค. appendChild ์ ๋ค๋ฅธ์ ์ ๋
ธ๋ ๋ฟ๋ง ์๋๋ผ ์ฌ๋ฌ๊ฐ์ ๋
ธ๋๋ฅผ ํ๋ฒ์, ๊ทธ๋ฆฌ๊ณ ํ
์คํธ๋ ์์ ๋
ธ๋๋ก ํฌํจ์ํฌ ์ ์๋ค๋๊ฒ ์
๋๋ค.target.remove()
: target ์์๋ฅผ ์ ๊ฑฐํฉ๋๋ค.const myBtn = document.querySelector("button");
const myUl = document.querySelector("ul");
myBtn.addEventListener('click', function(){
for(let i=0; i < 5; i++){
const myLi = document.createElement('li');
const btnDel = document.createElement('button');
const btnTxt = document.createTextNode('๋ฒํผ');
btnDel.append(btnTxt);
btnDel.addEventListener('click', ()=>{
myLi.remove();
});
myLi.append('์ญ์ ํ๊ธฐ: ', btnDel);
myUl.appendChild(myLi);
}
});
<div id="parentElement">
<span id="childElement">hello guys~</span>
</div>
// parentElement.insertBefore(target, location); target์์๋ฅผ parentElement์ ์์์ธ location ์์น ์์ผ๋ก ์ด๋ํฉ๋๋ค.
var span = document.createElement("span");
var sibling = document.getElementById("childElement");
var parentDiv = document.getElementById("parentElement");
parentDiv.insertBefore(span, sibling);
DOM api๋ฅผ ์ด์ฉํ๋ฉด ์์ ์์ ๊ฐ์ ์ ๊ทผํ์ฌ ๊ฐ์ ๊ฐ์ ธ์ค๊ฑฐ๋, ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
<p></p>
<input type="text">
<button>Write Something!</button>
const myBtn = document.querySelector("button");
const myP = document.querySelector("p");
const myInput = document.querySelector("input");
myBtn.addEventListener('click', function(){
myP.textContent = myInput.value;
});
// input ์์์ 'input' ์ด๋ฒคํธ๋ฅผ ์ฐ๊ฒฐํ๋ฉด ์ค์๊ฐ์ผ๋ก ๊ฐ์ด ๋ฐ์๋๊ฒ ๋ง๋ค ์๋ ์์ต๋๋ค.
myInput.addEventListener('input', ()=>{
myP.textContent = myInput.value;
});
โญ์ค์โญ myP.innerHTML = "<strong>I'm Strong!!</strong>";
// innerHTML ์ ์์(element) ๋ด์ ํฌํจ๋ HTML ๋งํฌ์
์ ๊ฐ์ ธ์ค๊ฑฐ๋ ์ค์ ํฉ๋๋ค. ์ค์ํ ๊ธฐ๋ฅ์ innerHTML๋ก ๊ฐ์ ํ ๋นํ ๋, ๋งํฌ์
์ผ๋ก ๋ณํํ ์ ์๋ ๋ฌธ์์ด์ด ์๋ค๋ฉด ๋งํฌ์
์ผ๋ก ๋ง๋ค์ด ๋ณด์ฌ์ค๋ค๋ ๊ฒ ์
๋๋ค. ๋ง์ฝ ๊ทธ๋ฐ ๋ฌธ์์ด์ด ์๋ค๋ฉด ๊ทธ๋ฅ ๋ฌธ์์ด๋ง ์ปจํ
์ธ ๋ก ์ค์ ํฉ๋๋ค.
// innerText ์์ฑ์ ์์์ **๋ ๋๋ง๋** ํ
์คํธ ์ฝํ
์ธ ๋ฅผ ๋ํ๋
๋๋ค. (**๋ ๋๋ง๋**์ ์ฃผ๋ชฉํ์ธ์. ****innerText๋ ํ
์คํธ ๋ด์ ๋ฌธ๋ฒ์ ์ผ๋ก ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ ํ
์คํธ๊ฐ ์์ผ๋ฉด ์ฒ๋ฆฌ๊ฐ ๋๋ ๊ฒฐ๊ณผ๋ฌผ์ ํ
์คํธ๋ก ์ ๋ฌํฉ๋๋ค.)
// textContent ์์ฑ์ ๋
ธ๋์ ํ
์คํธ ์ฝํ
์ธ ๋ฅผ ํํํฉ๋๋ค. ์ปจํ
์ธ ๋ฅผ ๋จ์ํ ํ
์คํธ๋ก๋ง ๋ค๋ฃน๋๋ค.
โญ์ค์โญ innerHTML ์ฌ์ฉ์ ์ฃผ์ ์ฌํญhttps://developer.mozilla.org/ko/docs/Web/API/Element/innerHTML#security_considerations
โญ์ค์โญ ๐ค innerHTML ๋ ์๊ฐํด๋ณด๊ธฐ ์์ ๋ด์ฉ์ ํตํด innerHTML์ด ์๋์น ์๊ฒ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์คํ์ํฌ ์ ์๋ค๋ ๊ฒ์ ์๊ฒ ๋์์ต๋๋ค. ๊ทธ๋ผ innerHTML์ ์ฌ์ฉํด์๋ ์๋๋ ์์ฑ์ผ๊น์? ๊ทธ๋ ์ง ์์ต๋๋ค. innerHTML์ ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด๊ณผ ์กฐํฉํด ๋ณต์กํ HTML ๊ตฌ์กฐ๋ ๋์ ์ผ๋ก ์์ฝ๊ฒ ์์ฑํ ์ ์๋ค๋ ์ฅ์ ์ด ์์ต๋๋ค. ๋จ, ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์๋์ํฌ ์ ์๋ ๊ฐ๋ฅ์ฑ์ด ์์ผ๋ ๋์ ์๋๋ฅผ ๊ฐ์ง ์ฌ์ฉ์๊ฐ ์ฝ๋๋ฅผ ์ ๋ ฅ ํ ์ ์๋๋ก ์ฌ์ฉ์์ ์ ๋ ฅ ๊ฐ์ innerHTML์ ํตํด ํ ๋น ๋ฐ๋ ์ผ๋ง ์๋๋ก ํ๋ฉด ์์ ํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋์ innerText ํน์ textContent ์์ฑ์ ์ด์ฉํฉ์๋ค.** innerText ์ textContent์ ์ฐจ์ด : https://developer.mozilla.org/ko/docs/Web/API/HTMLElement/innerText#์์
๐ค innerText ๋ ์๊ฐํด๋ณด๊ธฐ innerText ์์ฑ์ ์์์ **๋ ๋๋ง๋** ํ ์คํธ ์ฝํ ์ธ ๋ฅผ ๋ํ๋ธ๋ค๊ณ ํฉ๋๋ค. ํํ์ด ์ ๋งคํ๋ฐ์, mdn ์ ๋ฐ๋ฅด๋ฉด innerText๋ ์ฌ์ฉ์๊ฐ ์ปค์๋ฅผ ์ด์ฉํด ์์์ ์ฝํ ์ธ ๋ฅผ ์ ํํ๊ณ ํด๋ฆฝ๋ณด๋์ ๋ณต์ฌํ์ ๋ ์ป์ ์ ์๋ ํ ์คํธ์ ๊ทผ์ฟ๊ฐ์ ์ ๊ณตํ๋ค๊ณ ํฉ๋๋ค. ์ด ๋ง์ ์ฆ, ์๋ฅผ ๋ค์ด์ ๋ค์ด๋ฒ ๋ฉ์ธํ์ด์ง์ ํน์ ํ ์คํธ๋ฅผ ๋ณต์ฌ์์ ๋ฉ๋ชจ์ฅ์ ๋ถ์ฌ๋ฃ๊ธฐํ๋ค๊ณ ๊ฐ์ ํ์๋, ํ ์คํธ์ ์ค ๋ฐ๊ฟ๊ฐ์ ๊ฒ์ ๊ฐ์ ธ์ค์ง๋ง ํฐํธ์ ๊ตถ๊ธฐ๋ ์์๊น์ง ๊ฐ์ ธ์ค์ง ์๋ ๊ฒ๊ณผ ๊ฐ๋ค๊ณ ํ ์ ์๊ฒ ์ต๋๋ค.๋ฐ๋ผ์ innerText๋ display:none์ผ๋ก ์ธํด ์ ๊ฑฐ๋๋ ํ ์คํธ, br ํ๊ทธ๋ฅผ ํตํด ์ค ๋ฐ๊ฟ๋๋ ํ ์คํธ์ 'ํํ' ๋ ์ธ์ํ์ง๋ง color ๋ ๊ตต๊ธฐ, ์ฌ์ด์ฆ ๋ฑ ๊ณผ ๊ฐ์ ์ ๋ณด๋ ์ธ์ํ์ง ๋ชปํ๋ค๊ณ ๋ณผ ์ ์์ต๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ์ฌ ์์์ ์์ฑ์ ์ ์ดํ๋ ๋ฐฉ๋ฒ์ ๋ค์ํฉ๋๋ค. ๋ช ๊ฐ์ง ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค.
style
๊ฐ์ฒด์์๋ ๊ทธ ์์ CSSStyleDeclaration ๊ฐ์ฒด๋ผ ๋ถ๋ฆฌ๋ style ๊ฐ์ฒด๊ฐ ์กด์ฌํฉ๋๋ค. ์ด ๊ฐ์ฒด๋ ์์์ ์คํ์ผ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์คํ์ผ๊ณผ ๊ด๋ จํ ํ๋กํผํฐ์ ๋ฉ์๋๋ฅผ ์ง์ํฉ๋๋ค.
const target = document.querySelector("p");
const txtColor = target.style.color; // ํ์ฌ ์คํ์ผ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
target.style.color = "red"; // ํ์ฌ ์คํ์ผ ์ ๋ณด๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค.
target.style.fontWeight = "bold"; // ํ์ฌ ์คํ์ผ ์ ๋ณด์ font-weight ์์ฑ์ด ์๋ค๋ฉด ์ถ๊ฐํฉ๋๋ค.
target.style.color = null; // ํ์ฌ ์คํ์ผ ์ ๋ณด๋ฅผ ์ ๊ฑฐ(์ด๊ธฐํ)ํฉ๋๋ค.
๐ค **style ๊ฐ์ฒด์ ์์ฑ ์๋ณ์ ๊ท์น**
height
, color
โฆ)background-image
ย โ ย backgroundImage
)float
์์ฑ์ ๊ฒฝ์ฐ ์ด๋ฏธ ์๋ฐ์คํฌ๋ฆฝํธ์ ์์ฝ์ด๋ก ์กด์ฌํ๊ธฐ ๋๋ฌธ์ย cssFloat
์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค.style ๊ฐ์ฒด๋ฅผ ํตํด ์ค์ ๋ ์คํ์ผ์ CSS inline ์คํ์ผ๊ณผ ๋์ผํ ๊ฐ์ค์น๋ฅผ ๊ฐ์ง๋๋ค. ๋๋ฌธ์ CSS๋ฅผ ํตํด ์์ ์ ์ฌ์ง๊ฐ ์๋ ์คํ์ผ์๋ ๋ง์ด ์ฌ์ฉ๋์ง ์๋ ํธ์ ๋๋ค. ์ด๋ด๊ฒฝ์ฐ classList๋ฅผ ์ด์ฉํ ํด๋์ค ์ ์ด๊ฐ ๋ ํจ๊ณผ์ ์ ๋๋ค.
์์ฑ์ ์ ๊ทผํ๊ณ ์์ ํ ์ ์๋ Attribute ๋ฉ์๋
<p id='myTxt'>hello lions</p>
<img src='https://static.ebs.co.kr/images/public/lectures/2014/06/19/10/bhpImg/44deb98d-1c50-4073-9bd7-2c2c28d65f9e.jpg'>
<script>
const target = document.querySelector('p');
const myimg = document.querySelector('img');
const idAttr = target.getAttribute('id');
console.log(idAttr);
myimg.setAttribute("src", "https://img.wendybook.com/image_detail/img159/159599_01.jpg");
</script>
์์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋๋ก ๋์์ฃผ๋ data ์์ฑ
data-* ์์ฑ์ ์ฌ์ฉํ๋ฉด HTML ์์์ ์ถ๊ฐ์ ์ธ ์ ๋ณด๋ฅผ ์ ์ฅํ์ฌ ๋ง์น ํ๋ก๊ทธ๋จ ๊ฐ๋ฅํ ๊ฐ์ฒด์ฒ๋ผ ์ฌ์ฉํ ์ ์๊ฒ ํฉ๋๋ค. ๋จ, data ์์ฑ์ ์ด๋ฆ์๋ ์ฝ๋ก (:) ์ด๋ ์๋ฌธ ๋๋ฌธ์๊ฐ ๋ค์ด๊ฐ์๋ ์๋ฉ๋๋ค.
<img
class="terran battle-cruiser"
src="battle-cruiser.png"
data-ship-id="324"
data-weapons="laser"
data-health="400"
data-mana="250"
data-skill="yamato-cannon"
/>
<script>
const img = document.querySelector('img')
console.log(img.dataset);
console.log(img.dataset.shipId);
</script>
insertAdjacentHTML : ์์ ๋ ธ๋๋ฅผ ๋์์ ์ธ์ ํ ์ฃผ๋ณ์ ๋ฐฐ์นํฉ๋๋ค.
<strong class="sayHi">
๋ฐ๊ฐ์ต๋๋ค.
</strong>
const sayHi = document.querySelector('.sayHi');
sayHi.insertAdjacentHTML('beforebegin', '<span>์๋
ํ์ธ์ ์ ๋</span>');
sayHi.insertAdjacentHTML('afterbegin', '<span>์ฌํ์
๋๋ค</span>');
sayHi.insertAdjacentHTML('beforeend', '<span>๋ฉด์ ์ค์๋ฉด</span>');
sayHi.insertAdjacentHTML('afterend', '<span>์นํจ์ฌ๋๋ฆด๊ฒ์</span>');
์ฌ๊ธฐ์ begin
์ด๋ ์ฌ๋ ํ๊ทธ๋ฅผ, end
๋ ๋ซ๋ ํ๊ทธ๋ฅผ ์๋ฏธํฉ๋๋ค.
<!-- ์ฃผ์์
๋๋ค ์ฃผ์. -->
<article class="cont">
<h1>์๋
ํ์ธ์ ์ ๋ ์ด๋ฐ ์ฌ๋์
๋๋ค.</h1>
<p>์ง๊ธ๋ถํฐ ์๊ธฐ์๊ฐ ์ฌ๋ฆฌ๊ฒ ์ต๋๋ค</p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deserunt incidunt voluptates laudantium fugit, omnis
dolore itaque esse exercitationem quam culpa praesentium, quisquam repudiandae aut. Molestias qui quas ea iure
officiis.
<strong>๊ฐ์ฌํฉ๋๋ค!</strong>
</article>
const cont = document.querySelector(".cont");
console.log(cont.firstElementChild); // ์ฒซ๋ฒ์งธ ์์์ ์ฐพ์ต๋๋ค.
console.log(cont.lastElementChild); // ๋ง์ง๋ง ์์์ ์ฐพ์ต๋๋ค.
console.log(cont.nextElementSibling); // ๋ค์ ํ์ ์์๋ฅผ ์ฐพ์ต๋๋ค.
console.log(cont.previousSibling); // ์ด์ ํ์ ๋
ธ๋๋ฅผ ์ฐพ์ต๋๋ค.
console.log(cont.children); // ๋ชจ๋ ์์์์๋ฅผ ์ฐพ์ต๋๋ค.
console.log(cont.childNodes); // ๋ชจ๋ ์์๋
ธ๋๋ฅผ ์ฐพ์ต๋๋ค.
console.log(cont.parentElement); // ๋ถ๋ชจ ์์๋ฅผ ์ฐพ์ต๋๋ค.
// ์๊ธฐ ์์ ๋ถํฐ ์์ํด ๋ถ๋ชจ๋ก ํ๊ณ ์ฌ๋ผ๊ฐ๋ฉฐ ๊ฐ์ฅ ๊ฐ๊น์ด cont ํด๋์ค ์์๋ฅผ ์ฐพ์ต๋๋ค. ๋จ, ํ์ ์์๋ ์ฐพ์ง ์์ต๋๋ค.
console.log(cont.querySelector('strong').closest('.cont').innerHTML);
์ด๋ฒคํธ์์ ํธ์ถ๋๋ ํธ๋ค๋ฌ์๋ ์ด๋ฒคํธ์ ๊ด๋ จ๋ ๋ชจ๋ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ ๋งค๊ฐ๋ณ์๊ฐ ์ ์ก๋ฉ๋๋ค. ์ด๊ฒ์ด ๋ฐ๋ก ์ด๋ฒคํธ ๊ฐ์ฒด์ ๋๋ค!
<article class="parent">
<ol>
<li><button class="btn-first" type="button">๋ฒํผ1</button></li>
<li><button type="button">๋ฒํผ2</button></li>
<li><button type="button">๋ฒํผ3</button></li>
</ol>
</article>
const btnFirst = document.querySelector('.btn-first');
btnFirst.addEventListener('click', (***event***) => {
console.log(event);
});
๋ธ๋ผ์ฐ์ ํ๋ฉด์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ๋ธ๋ผ์ฐ์ ๋ ๊ฐ์ฅ ๋จผ์ ์ด๋ฒคํธ ๋์์ ์ฐพ๊ธฐ ์์ํฉ๋๋ค.
๋ธ๋ผ์ฐ์ ๊ฐ ์ด๋ฒคํธ ๋์์ ์ฐพ์๊ฐ ๋๋ ๊ฐ์ฅ ์์์ window ๊ฐ์ฒด๋ถํฐ document, body ์์ผ๋ก DOM ํธ๋ฆฌ๋ฅผ ๋ฐ๋ผ ๋ด๋ ค๊ฐ๋๋ค. ์ด๋ฅผ ์บก์ฒ๋ง ๋จ๊ณ๋ผ๊ณ ํฉ๋๋ค.
์ด๋ ์ด๋ฒคํธ ๋์์ ์ฐพ์๊ฐ๋ ๊ณผ์ ์์ ๋ธ๋ผ์ฐ์ ๋ ์ค๊ฐ์ ๋ง๋๋ ๋ชจ๋ ์บก์ฒ๋ง ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์คํ์ํต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด๋ฒคํธ ๋์์ ์ฐพ๊ณ ์บก์ฒ๋ง์ด ๋๋๋ฉด ์ด์ ๋ค์ DOM ํธ๋ฆฌ๋ฅผ ๋ฐ๋ผ ์ฌ๋ผ๊ฐ๋ฉฐ ๋ง๋๋ ๋ชจ๋ ๋ฒ๋ธ๋ง ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์คํํฉ๋๋ค. ์ด๋ฅผ ์ด๋ฒคํธ ๋ฒ๋ธ๋ง ๋จ๊ณ๋ผ๊ณ ํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ฌํ ๊ณผ์ ์์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๊ฐ ์ฐจ๋ก๋ก ์คํ๋๋๊ฒ์ ์ด๋ฒคํธ ์ ํ(event propagation)๋ผ๊ณ ํฉ๋๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="../reset.css">
<style>
</style>
</head>
<body>
<article class="parent">
<button class="btn" type="button">๋ฒํผ</button>
</article>
<script>
const parent = document.querySelector('.parent');
const btnFirst = document.querySelector('.btn');
btnFirst.addEventListener('click', (event) => {
console.log("btn capture!");
})
window.addEventListener('click', () => {
console.log("window capture!");
}, true); // true : ์บก์ฒ๋ง ๋จ๊ณ์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋๋ก ํฉ๋๋ค.
document.addEventListener('click', () => {
console.log("document capture!");
}, true);
parent.addEventListener('click', () => {
console.log("parent capture!");
}, true);
btnFirst.addEventListener('click', (event) => {
console.log("btn bubble!");
})
parent.addEventListener('click', () => {
console.log("parent bubble!");
});
document.addEventListener('click', () => {
console.log("document bubble!");
});
window.addEventListener('click', () => {
console.log("window bubble!");
});
</script>
</body>
</html>
๋ถ๋ชจ๋ถํฐ ์์๊น์ง ์ผ๋ จ์ ์์๋ฅผ ๋ชจ๋ ํ๊ณ ๊ฐ๋ฉฐ ์งํ๋๋ ์ด๋ฌํ ์ด๋ฒคํธ์ ํน์ง ๋๋ถ์ ์ด๋ฒคํธ ๊ฐ์ฒด์๋ target, currentTarget ์ด๋ผ๋ ์์ฑ์ด ์กด์ฌํฉ๋๋ค.
target ์์ฑ์๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์ง์์ง์ ์ ๋ณด๊ฐ ๋ด๊ฒจ ์์ต๋๋ค. target ์์ฑ์ ํตํด ์ด๋ฒคํธ ๋ฆฌ์ค๋๊ฐ ์๋ ์์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋๋ ํด๋น ์์์ ์ ๊ทผ ํ ์ ์์ต๋๋ค.
currentTarget ์์ฑ์๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋๊ฐ ์ฐ๊ฒฐ๋ ์์๊ฐ ์ฐธ์กฐ๋์ด ์์ต๋๋ค.
<article class="parent">
<ol>
<li><button class="btn-first" type="button">๋ฒํผ1</button></li>
<li><button type="button">๋ฒํผ2</button></li>
<li><button type="button">๋ฒํผ3</button></li>
</ol>
</article>
<script>
const parent = document.querySelector('.parent');
parent.addEventListener('click', function (event) {
console.log(event.target);
console.log(event.currentTarget);
})
</script>
์์์ ์ฐ๋ฆฌ๋ ์ด๋ฒคํธ์ ํ๋ฆ์ ํตํด ์ด๋ฒคํธ ๋ฆฌ์ค๋๊ฐ ์๋ ์์์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋๋ ํด๋น ์์์ ์ ๊ทผ ํ ์ ์๋ค๋ ๊ฒ์ ์๊ฒ ๋์์ต๋๋ค.
์ด๋ฅผ ์ด์ฉํด ์ด๋ฒคํธ ๋ฆฌ์ค๋๊ฐ ์์ด๋ ๋ง์น ๋ฆฌ์ค๋๊ฐ ์๋ ๊ฒ ์ฒ๋ผ ์ฌ์ฉ ํ ์ ์์ต๋๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="../reset.css">
<style>
</style>
</head>
<body>
<article class="parent">
<ol>
<li><button class="btn-first" type="button">๋ฒํผ1</button></li>
<li><button type="button">๋ฒํผ2</button></li>
<li><button type="button">๋ฒํผ3</button></li>
</ol>
</article>
<script>
const parent = document.querySelector('.parent');
parent.addEventListener('click', function (event) {
console.log(event.target);
if (event.target.nodeName === "BUTTON") {
event.target.textContent = "๋ฒํผ4";
}
})
</script>
</body>
</html>
์ด๋ฌํ ํ ํฌ๋์ ์ด๋ฒคํธ ์์์ด๋ผ๊ณ ํฉ๋๋ค.
์ด๋ฒคํธ ๋ฆฌ์ค๋ ํจ์ ๋ด๋ถ์์์ this ๊ฐ์ ์ด๋ฒคํธ๊ฐ ์ฐ๊ฒฐ๋ ๋ ธ๋๋ฅผ ์ฐธ์กฐํฉ๋๋ค.
<article class="parent">
<ol>
<li><button class="btn-first" type="button">๋ฒํผ1</button></li>
<li><button type="button">๋ฒํผ2</button></li>
<li><button type="button">๋ฒํผ3</button></li>
</ol>
</article>
<script>
const parent = document.querySelector('.parent');
parent.addEventListener('click', function (event) {
console.log(this);
});
const myObj = {
name: 'jaehyun',
walk() {
parent.addEventListener('click', () => {
console.log(this.name + ' is walking');
})
}
}
</script>
์ด๋ event.currentTarget ์์ฑ์ ์ฐธ์กฐ๊ฐ๊ณผ ์ ์ฌํฉ๋๋ค.
๋ง์ฝ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ํจ์๋ฅผ ํ์ดํ ํจ์๋ก ์ด๋ค๋ฉด this ๊ฐ ๊ฐ๋ฆฌํค๋ ๋์์ด ๋ฌ๋ผ์ง๋ค๋ ์ ์ ์ ์ํ์ธ์.
๋ธ๋ผ์ฐ์ ์ ๊ธฐ๋ณธ ์ด๋ฒคํธ ๋์์ ์ทจ์ํฉ๋๋ค.
๋ธ๋ผ์ฐ์ ๋ HTML ํ๊ทธ๋ฅผ ํตํด ์ฌ๋ฌ๊ฐ์ง ๊ธฐ๋ฅ๋ค์ ์ ๊ณตํฉ๋๋ค. ํ์ง๋ง ๋๋๋ก ๊ทธ๋ฌํ ๊ธฐ๋ฅ์ด ๋ฐฉํด๊ฐ ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค.
<!-- ์ต์ปค์ ๊ธฐ๋ณธ ๋์์ ์ค์ง -->
<a href="https://www.naver.com" class="link">๋ค์ด๋ฒ ๋งํฌ์
๋๋ค๋ง..</a>
<script>
const link = document.querySelector('.link');
link.addEventListener('click', (event) => {
console.log('clicked');
event.preventDefault();
})
</script>
<!-- submit ์ ๊ธฐ๋ณธ ๋์์ ์ค์ง -->
<form action="">
<button type="submit" class="submit">์ ์ถ</button>
</form>
<script>
const submit = document.querySelector('.submit');
submit.addEventListener('click', (event) => {
console.log('clicked');
event.preventDefault();
})
</script>
์ด๋ ๋ฏ ์ข ์ข ๋ธ๋ผ์ฐ์ ์ ๊ธฐ๋ณธ ๋์์ ์ค์งํ๊ณ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ํตํด ๊ธฐ๋ฅ์ ์ฒ๋ฆฌํ๊ณ ์ ํ ๋ ์ฌ์ฉํฉ๋๋ค.
preventDefault ์ค์ต
์ด๋ฒคํธ ํ์ contextmenu๋ฅผ ์ด์ฉํด ๋ธ๋ผ์ฐ์ ์ค๋ฅธ์ชฝ ๋ฒํผ ์ด๋ฒคํธ ๋ง๊ณ 'ํด๋น ํ์ด์ง์์๋ ์ค๋ฅธ์ชฝ ํด๋ฆญ์ ์ ํํฉ๋๋ค.โ ๊ฒฝ๊ณ ๋์ฐ๊ธฐ
์์์ ์ฐ๋ฆฌ๋ preventDefault๋ฅผ ํตํด ๋ธ๋ผ์ฐ์ ์ ๊ธฐ๋ณธ ์ด๋ฒคํธ ๋์์ ์ทจ์ํด๋ดค์ต๋๋ค. ํ์ง๋ง ์ด๋ ์ฐ๋ฆฌ๊ฐ ์์์ ๋ฐฐ์ ๋ ์ด๋ฒคํธ ํ๋ฆ, ์ฆ ์ด๋ฒคํธ ์ ํ(์ด๋ฒคํธ ํ๋กํ๊ฒ์ด์ )๋ฅผ ๋ง์ง๋ ๋ชปํฉ๋๋ค.
<form action="">
<button type="submit" class="submit">์ ์ถ</button>
</form>
<script>
const submit = document.querySelector('.submit');
submit.addEventListener('click', (event) => {
console.log('clicked');
event.preventDefault();
// event.stopPropagation();
});
document.body.addEventListener('click', () => {
console.log('event still alive!');
});
</script>
์ด๋ ๋ง์ฝ ์ด๋ฒคํธ ์ ํ๋ฅผ ๋ง๊ณ ์ถ๋ค๋ฉด event.stopPropagation()
์ฝ๋๋ฅผ ์ถ๊ฐํฉ๋๋ค.
stopPropagation() ์ค์ต
<h1>๋์ todo list</h1>
<p>1. ์ค๋ ์ ๋
์๋ ๋ถ๋์ฐ๊ฒ๋ฅผ ๋์ฌ ๋จน๊ฒ ๋ค.<button type="button">์ญ์ </button></p>
<p>2. ํ์์ผ๋ก ์ํ
์คํ๋ฅผ ๋จน๊ฒ ๋ค.<button type="button">์ญ์ </button></p>
<p>3. ์๊ธฐ ์ ์ ๋ฐ๋์ ๋ด์ผ ์์นจ ๋ฉ๋ด๋ฅผ ์๊ฐํด๋๊ฒ ๋ค.<button type="button">์ญ์ </button></p>
์์ ๊ฐ์ HTML์ฝ๋ ๊ตฌ์กฐ๊ฐ ์์ ๋