: Dom (=Document Object Model) (=๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ)
: HTML ๋ฌธ์ ์ ์ฒด๋ฅผ ๊ฐ์ฒด๋ก ํํํ ๊ฒ.
: HTML์ ๊ณ์ธตํ์์ผ ํธ๋ฆฌ๊ตฌ์กฐ๋ก ๋ง๋ ๊ฐ์ฒด(Object) ๋ชจ๋ธ์
๋๋ค.
(๊ทธ๋ฆผ ์ถ์ฒ: http://www.tcpschool.com/javascript/js_dom_node)
<div class="greeting"> Hello </div>
// <div></div> โ์์๋
ธ๋
// Hello โํ
์คํธ ๋
ธ๋
// class="greeting" โ์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋
๋ฉ์๋ | ์๋ฏธ | ๊ฒฐ๊ณผ |
---|---|---|
document.getElementById(โidโ) | ID์์ฑ์ผ๋ก ํ๊ทธ ์ ํํ๊ธฐ | ID์ ํด๋นํ๋ ํ๊ทธ ํ๋ |
document.getElementsByClassName(โclassโ) | class ์์ฑ์ผ๋ก ํ๊ทธ ์ ํํ๊ธฐ | class์ ํด๋นํ๋ ํ๊ทธ ๋ชจ์ (HTMLCollection) ์ ์ฌ๋ฐฐ์ด |
document.getElementsByTagName(โtagโ) | HTML ํ๊ทธ ์ด๋ฆ์ผ๋ก ํ๊ทธ ์ ํํ๊ธฐ | tag์ ํด๋นํ๋ ํ๊ทธ ๋ชจ์ (HTMLCollection) ์ ์ฌ๋ฐฐ์ด |
document.querySelector(โcssโ) | css ์ ํ์๋ก ํ๊ทธ ์ ํํ๊ธฐ | css ์ ํ์์ ํด๋นํ๋ ํ๊ทธ ์ค โ๊ฐ์ฅ ์ฒซ๋ฒ์งธโ ํ๊ทธ ํ๋ |
document.querySelectorAll(โcssโ) | css ์ ํ์๋ก ํ๊ทธ ์ ํํ๊ธฐ | css ์ ํ์์ ํด๋นํ๋ ํ๊ทธ๋ชจ์ (NodeList)์ ์ฌ๋ฐฐ์ด |
โป getElements , querySelectorAll ์ฒ๋ผ ์ฌ๋ฌ๊ฐ ์์๊ฐ ๋์ค๋ ๊ฒฝ์ฐ โ ์ ์ฌ๋ฐฐ์ด๋ก ๋์ด
ํ๋กํผํฐ | ์ค๋ช |
---|---|
element.children | ์์ ๋ ธ๋ ์ค ์์ ๋ ธ๋๋ง HTMLCollection์ ๋ด์์ ๋ฐํ. (ํ ์คํธ ๋ ธ๋๋ ํฌํจ๋์ง ์์.) |
element.firstElementChild | ์ฒซ ๋ฒ์งธ ์์ ์์ ๋ ธ๋๋ง ๋ฐํ |
element.lastElementChild | ๋ง์ง๋ง ์์ ์์ ๋ ธ๋๋ง ๋ฐํ |
element.parentElement | ๋ถ๋ชจ ์์ ๋ ธ๋๋ง ๋ฐํ |
element.previousElementSibling | ์์ ์ ์ด์ ํ์ ์์ ๋ ธ๋๋ง ๋ฐํ |
element.nextElementSibling | ์์ ์ ๋ค์ ํ์ ์์ ๋ ธ๋๋ง ๋ฐํ |
Node.prototype.childNodes | Element.prototype.children |
---|---|
Node์ ํ๋กํผํฐ๋ ๋๋ถ๋ถ "ํ ์คํธ ๋ ธ๋"์ด๊ฑฐ๋ "์์ ๋ ธ๋"์. | Element์ ํ๋กํผํฐ๋ ๋ค "์์๋
ธ๋" ์. (ํ ์คํธ ๋ ธ๋๋ ๋ฐํ๋์ง ์์) |
<div id="wrap">
<h1>Rainbow</h1>
<ul id="rainbow">
<li><span>color1 </span>red</li>
<li><span>color2 </span>orange</li>
<li><span>color3 </span>yellowgreen</li>
<li><span>color4 </span>green</li>
<li><span>color5 </span>blue</li>
<li><span>color6 </span>darkblue</li>
<li><span>color7 </span>purple</li>
</ul>
<h5>A rainbow is caused by sunlight and atmospheric conditions.</h5>
</div>
const rainbowEl = document.querySelector("#rainbow");
// 1. element.children ์ Node.childNodes
const ChildEl = rainbowEl.children; // HTMLCollection(7) [li, li, li, li, li, li, li]
const ChildNd = rainbowEl.childNodes; // NodeList(15) [text, li, text, li, text, li, text, li, text, li, text, li, text, li, text]
// 2. element.firstElementChild ์ Node.firstChild
const firstChildEl = rainbowEl.firstElementChild; // <li><span>color1 </span>red</li>
const firstNd = rainbowEl.firstChild; // #text
// 3. element.lastElementChild ์ Node.lastChild
const lastEl = rainbowEl.lastElementChild; // <li><span>color7 </span>purple</li>
const lastNd = rainbowEl.lastChild; // #text
// 4. element.parentElement ์ Node.parentNode
const parentEl = rainbowEl.parentElement; // <div id="wrap"> ... </div>
const parentNd = rainbowEl.parentNode; // <div id="wrap"> ... </div>
// 5. element.previousElementSibling ์ Node.previousSibling
const prevEl = rainbowEl.previousElementSibling; //
<h1>Rainbow</h1>
const prevNd = rainbowEl.previousSibling; // #text
//6. element.nextElementSibling ์ Node.nextSibling
const nextEl = rainbowEl.nextElementSibling; // <h5>...</h5>
const nextNd = rainbowEl.nextSibling; // #text
ํ๋กํผํฐ | ์ค๋ช |
---|---|
prepend | ์ ํํ์์์ ์์์์ ์์ ๋ด์ฉ์ฝ์ |
append | ์ ํํ์์์ ์์์์ ๋ค์ ๋ด์ฉ์ฝ์ |
before | ์ ํํ ์์์ ์์ ๋ด์ฉ ์ฝ์ |
after | ์ ํํ ์์์ ๋ค์ ๋ด์ฉ ์ฝ์ |
ํ๋กํผํฐ | ์ค๋ช |
---|---|
remove |
<div id="todos">
<!--before-->
<ul id="today"> <!--๊ธฐ์ค-->
<!--prepend-->
<li>๋๊ธฐ</li>
<li>์จ์ฌ๊ธฐ</li>
<li>์ถค์ถ๊ธฐ</li>
<!--append-->
</ul>
<!--after-->
<hr/>
<ul id="tomorrow">
<li>์ ์๊ธฐ</li>
<li>์ฐ์ฑ
๊ฐ๊ธฐ</li>
<li>์ฅ๋ณด๊ธฐ</li>
</ul>
</div>
/* <๋
ธ๋์ด๋> */
const todayEl = document.querySelector('#today');
const tomorrowEl = document.querySelector('#tomorrow');
// 1.prepend
// tomorrow์ ์ฒซ๋ฒ์งธ li๋ฅผ today์ ์ฒซ๋ฒ์จฐ li๋ก ์ฎ๊ธฐ๊ธฐ
todayEl.prepend(tomorrowEl.children[0]);
// 2.append
// today์ ๋ง์ง๋ง li๋ฅผ tomorrow์ ๋ง์ง๋ง li๋ก ์ฎ๊ธฐ๊ธฐ
tomorrowEl.append(todayEl.children[3]);
// 3.before
//today ul์์ h2ํ๊ทธ ๋ฃ๊ธฐ
const pTitle = document.createElement('h2');
pTitle.innerHTML = 'today';
todayEl.before(pTitle);
// 4.after
// tomorrow ul๋ค์ h2ํ๊ทธ ๋ฃ๊ธฐ
const tomoTitle = document.createElement('h2');
tomoTitle.innerHTML = 'tomorrow';
tomorrowEl.after(tomoTitle);
/* <๋
ธ๋์ญ์ >*/
// 1.remove()
// tomoTitle์ญ์ ํ๊ธฐ
tomoTitle.remove();
append | appendChild |
---|---|
"์ฌ๋ฌ๊ฐ" ์์์์ ์ค์ ๊ฐ๋ฅ | "ํ๋๋ง" ๊ฐ๋ฅ |
"html์์" , "๋ฌธ์์ด"์ ๋ฃ์ | "html์์"๋ง ๊ฐ๋ฅ |
1.์์ฑ ์ถ๊ฐ(์์ )ํ๊ธฐ: element.setAttribute('์์ฑ', '๊ฐ')
2.์์ฑ์ ์ ๊ทผํ๊ธฐ: element.getAttribute('์์ฑ')
3.์์ฑ ์ ๊ฑฐํ๊ธฐ: element.removeAttribute('์์ฑ')
const aEl = document.querySelector('a');
// 1.์์ฑ ์ถ๊ฐ(์์ )ํ๊ธฐ: element.setAttribute('์์ฑ', '๊ฐ')
aEl.setAttribute('href', 'http://www.naver.com');
// 2.์์ฑ์ ์ ๊ทผํ๊ธฐ: element.getAttribute('์์ฑ')
console.log(aEl.getAttribute('href')); // http://www.naver.com
// 3.์์ฑ ์ ๊ฑฐํ๊ธฐ: element.removeAttribute('์์ฑ')
aEl.removeAttribute('href');
: class ์ ๋ณด๋ฅผ ๋ด์ DOMTokenList ๊ฐ์ฒด๋ฅผ ๋ฐํํจ.
โป DOMTokenList๊ฐ์ฒด : ์ ์ฌ๋ฐฐ์ด, ์ดํฐ๋ฌ๋ธ์.
<ol class="c1">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ol>
const olEl = document.querySelector('.c1');
// 1.add - ํด๋์ค ์ถ๊ฐํ๊ธฐ
olEl.classList.add('c2', 'c3');
console.log(olEl.classList); // ['c1', 'c2', 'c3]
// 2.toggle - ํด๋์ค ์์ผ๋ฉด ์ญ์ , ์์ผ๋ฉด ์ถ๊ฐํ๊ธฐ
olEl.classList.toggle('c4');
olEl.classList.toggle('c3');
console.log(olEl.classList); // ['c1', 'c2', 'c4']
// 3.remove - ํด๋์ค ์ญ์ ํ๊ธฐ
olEl.classList.remove('c4');
console.log(olEl.classList); // ['c1', 'c2']
1. innerHTML
1)์กฐํ : HTML ๋งํฌ์
์ด ํฌํจ๋ ๋ฌธ์์ด์ ๊ทธ๋๋ก ๋ฐํํจ. (text๋ฟ ์๋๋ผ , html์์๋ ๋ฐํํจ.)
2) ์
๋ ฅ : ๋ฌธ์์ด์ด "์์ ๋
ธ๋"๋ก ๋ค์ด๊ฐ.
2. textContent
1)์กฐํ : HTML ๋งํฌ์
์ ๋ฌด์ํ๊ณ ํ
์คํธ๋ง ๋ฐํํจ.
2)์
๋ ฅ : ๋ฌธ์์ด์ด "ํ
์คํธ"๋ก ๊ทธ๋๋ก ๋ค์ด๊ฐ
<body>
<div id="hello">
Hello <span>World</span>
</div>
</body>
const helloEl = document.querySelector('#hello');
// 1.์กฐํ
console.log(helloEl.innerHTML); // Hello <span>World</span>
console.log(helloEl.textContent); // Hello World
// 2.์
๋ ฅ
helloEl.innerHTML = '<b>innerHTML</b>'; // <b>innerHTML</b>์ด ์์๋
ธ๋๋ก ๋ค์ด๊ฐ
helloEl.textContent = '<b>innerHTML</b>'; // <b>innerHTML</b> ์ด ๋ฌธ์ ๊ทธ๋๋ก ๋ค์ด๊ฐ
html์์ ํํฌ ์์ฑ์ผ๋ก data-**๋ฅผ ์ง์ ํ๋ฉด, js์์ dataset-** ์ผ๋ก ๊ทธ ์์ฑ์ ์ฌ์ฉํ ์ ์์.
data-๋ก ์์ํ๋ ์์ฑ์ ๋ชจ๋ dataset์ด๋ผ๋ ํ๋กํผํฐ์ ์ ์ฅ๋จ.
ex) data-status๋ผ๋ ์์ฑ์ด ์๋ค๋ฉด, element.dataset.status๋ผ๋ ํ๋กํผํฐ์ ์ ๊ทผํด์ ๊ทธ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ ๊ฒ์.
(mdn์๋ฃ)
์ฃผ์
:์ธํฐ๋ท ์ต์คํ๋ก๋ฌ11+ ์ด์ ๋ฒ์ ๋ค์ dataset์ ์ง์ํ์ง ์์.
IE 10 ์ดํ์์ getAttribute()๋ฅผ ํตํด ๋ฐ์ดํฐ ์์ฑ์ ์ ๊ทผํด์ผ ํจ.