[javascript] DOM
in JavaScript
DOM(Document Object Model)
ํ ์คํธ ํ์ผ๋ก ๋ง๋ค์ด์ ธ ์๋ ์น ๋ฌธ์๋ฅผ ๋ธ๋ผ์ฐ์ ์ ๋ ๋๋งํ๋ ค๋ฉด ์น ๋ฌธ์๋ฅผ ๋ธ๋ผ์ฐ์ ๊ฐ ์ดํดํ ์ ์๋ ๊ตฌ์กฐ๋ก ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ ค์ผ ํ๋ค. ๋ธ๋ผ์ฐ์ ์ ๋ ๋๋ง ์์ง์ ์น ๋ฌธ์๋ฅผ ๋ก๋ํ ํ, ํ์ฑํ์ฌ ์น ๋ฌธ์๋ฅผ ๋ธ๋ผ์ฐ์ ๊ฐ ์ดํดํ ์ ์๋ ๊ตฌ์กฐ๋ก ๊ตฌ์ฑํ์ฌ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฌํ๋๋ฐ ์ด๋ฅผ DOM์ด๋ผ ํ๋ค. ์ฆ ๋ชจ๋ ์์์ ์์์ ์ดํธ๋ฆฌ๋ทฐํธ, ํ ์คํธ๋ฅผ ๊ฐ๊ฐ์ ๊ฐ์ฒด๋ก ๋ง๋ค๊ณ ์ด๋ค ๊ฐ์ฒด๋ฅผ ๋ถ์ ๊ด๊ณ๋ฅผ ํํํ ์ ์๋ ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ๊ตฌ์ฑํ ๊ฒ์ด DOM์ด๋ค. ์ด DOM์ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ํตํด ๋์ ์ผ๋ก ๋ณ๊ฒฝํ ์ ์์ผ๋ฉฐ ๋ณ๊ฒฝ๋ DOM์ ๋ ๋๋ง์ ๋ฐ์๋๋ค.
์ด๋ฌํ ์น ๋ฌธ์์ ๋์ ๋ณ๊ฒฝ์ ์ํด DOM์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๊ฐ ์์ ์ ์ ๊ทผํ๊ณ ์์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋๋ฐ ์ผ๋ฐ์ ์ผ๋ก ํ๋กํผํฐ์ ๋ฉ์๋๋ฅผ ๊ฐ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ก ์ ๊ณต๋๋ค. ์ด๋ฅผ DOM API(Application Programming Interface)๋ผ๊ณ ๋ถ๋ฅธ๋ค. ๋ฌ๋ฆฌ ๋งํ๋ฉด ์ ์ ์ธ ์นํ์ด์ง์ ์ ๊ทผํ์ฌ ๋์ ์ผ๋ก ์นํ์ด์ง๋ฅผ ๋ณ๊ฒฝํ๊ธฐ ์ํ ์ ์ผํ ๋ฐฉ๋ฒ์ ๋ฉ๋ชจ๋ฆฌ ์์ ์กด์ฌํ๋ DOM์ ๋ณ๊ฒฝํ๋ ๊ฒ์ด๊ณ , ์ด๋ ํ์ํ ๊ฒ์ด DOM์ ์ ๊ทผํ๊ณ ๋ณ๊ฒฝํ๋ ํ๋กํผํฐ์ ๋ฉ์๋์ ์งํฉ์ธ DOM API์ด๋ค.
DOM์ HTML, ECMAScript์์ ์ ์ํ ํ์ค์ด ์๋ ๋ณ๊ฐ์ W3C์ ๊ณต์ ํ์ค์ด๋ฉฐ ํ๋ซํผ/ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ์ค๋ฆฝ์ ์ด๋ค. DOM์ ๋ค์ ๋ ๊ฐ์ง ๊ธฐ๋ฅ์ ๋ด๋นํ๋ค.
HTML ๋ฌธ์์ ๋ํ ๋ชจ๋ธ ๊ตฌ์ฑ
๋ธ๋ผ์ฐ์ ๋ HTML ๋ฌธ์๋ฅผ ๋ก๋ํ ํ ํด๋น ๋ฌธ์์ ๋ํ ๋ชจ๋ธ์ ๋ฉ๋ชจ๋ฆฌ์ ์์ฑํ๋ค. ์ด๋ ๋ชจ๋ธ์ ๊ฐ์ฒด์ ํธ๋ฆฌ๋ก ๊ตฌ์ฑ๋๋๋ฐ ์ด๊ฒ์ DOM tree๋ผ ํ๋ค.
HTML ๋ฌธ์ ๋ด์ ๊ฐ ์์์ ์ ๊ทผ / ์์
DOM์ ๋ชจ๋ธ ๋ด์ ๊ฐ ๊ฐ์ฒด์ ์ ๊ทผํ๊ณ ์์ ํ ์ ์๋ ํ๋กํผํฐ์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค. DOM์ด ์์ ๋๋ฉด ๋ธ๋ผ์ฐ์ ๋ฅผ ํตํด ์ฌ์ฉ์๊ฐ ๋ณด๊ฒ ๋ ๋ด์ฉ ๋ํ ๋ณ๊ฒฝ๋๋ค.
DOM Tree
DOM tree๋ ๋ธ๋ผ์ฐ์ ๊ฐ HTML ๋ฌธ์๋ฅผ ๋ก๋ํ ํ ํ์ฑํ์ฌ ์์ฑํ๋ ๋ชจ๋ธ์ ์๋ฏธํ๋ค. ๊ฐ์ฒด์ ํธ๋ฆฌ๋ก ๊ตฌ์กฐํ๋์ด ์๊ธฐ ๋๋ฌธ์ DOM tree๋ผ ๋ถ๋ฅธ๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
.red { color: #ff0000; }
.blue { color: #0000ff; }
</style>
</head>
<body>
<div>
<h1>Cities</h1>
<ul>
<li id="one" class="red">Seoul</li>
<li id="two" class="red">London</li>
<li id="three" class="red">Newyork</li>
<li id="four">Tokyo</li>
</ul>
</div>
</body>
</html>
DOM์์ ๋ชจ๋ ์์, ์ดํธ๋ฆฌ๋ทฐํธ, ํ ์คํธ๋ ํ๋์ ๊ฐ์ฒด์ด๋ฉฐ Document ๊ฐ์ฒด์ ์์์ด๋ค. ์์์ ์ค์ฒฉ๊ด๊ณ๋ ๊ฐ์ฒด์ ํธ๋ฆฌ๋ก ๊ตฌ์กฐํํ์ฌ ๋ถ์๊ด๊ณ๋ฅผ ํํํ๋ค. DOM tree์ ์ง์ ์ (Entry point)๋ document ๊ฐ์ฒด์ด๋ฉฐ ์ต์ข ์ ์ ์์์ ํ ์คํธ๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด์ด๋ค.
DOM tree๋ ๋ค ์ข ๋ฅ์ ๋ ธ๋๋ก ๊ตฌ์ฑ๋๋ค.
๋ฌธ์ ๋ ธ๋(Document Node) |
---|
ํธ๋ฆฌ์ ์ต์์์ ์กด์ฌํ๋ฉฐ ๊ฐ๊ฐ ์์, ์ดํธ๋ฆฌ๋ทฐํธ, ํ ์คํธ ๋ ธ๋์ ์ ๊ทผํ๋ ค๋ฉด ๋ฌธ์ ๋ ธ๋๋ฅผ ํตํด์ผ ํ๋ค. ์ฆ, DOM tree์ ์ ๊ทผํ๊ธฐ ์ํ ์์์ (entry point)์ด๋ค. |
์์ ๋ ธ๋(Element Node) |
---|
์์ ๋ ธ๋๋ HTML ์์๋ฅผ ํํํ๋ค. HTML ์์๋ ์ค์ฒฉ์ ์ํด ๋ถ์ ๊ด๊ณ๋ฅผ ๊ฐ์ง๋ฉฐ ์ด ๋ถ์ ๊ด๊ณ๋ฅผ ํตํด ์ ๋ณด๋ฅผ ๊ตฌ์กฐํํ๋ค. ๋ฐ๋ผ์ ์์ ๋ ธ๋๋ ๋ฌธ์์ ๊ตฌ์กฐ๋ฅผ ์์ ํ๋ค๊ณ ๋ง ํ ์ ์๋ค. ์ดํธ๋ฆฌ๋ทฐํธ, ํ ์คํธ ๋ ธ๋์ ์ ๊ทผํ๋ ค๋ฉด ๋จผ์ ์์ ๋ ธ๋๋ฅผ ์ฐพ์ ์ ๊ทผํด์ผ ํ๋ค. ๋ชจ๋ ์์ ๋ ธ๋๋ ์์๋ณ ํน์ฑ์ ํํํ๊ธฐ ์ํด HTMLElement ๊ฐ์ฒด๋ฅผ ์์ํ ๊ฐ์ฒด๋ก ๊ตฌ์ฑ๋๋ค. (๊ทธ๋ฆผ: DOM tree์ ๊ฐ์ฒด ๊ตฌ์ฑ ์ฐธ๊ณ ) |
์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋(Attribute Node) |
---|
์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋๋ HTML ์์์ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ํํํ๋ค. ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋๋ ํด๋น ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์ง์ ๋ ์์์ ์์์ด ์๋๋ผ ํด๋น ์์์ ์ผ๋ถ๋ก ํํ๋๋ค. ๋ฐ๋ผ์ ํด๋น ์์ ๋ ธ๋๋ฅผ ์ฐพ์ ์ ๊ทผํ๋ฉด ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ฐธ์กฐ, ์์ ํ ์ ์๋ค. |
ํ ์คํธ ๋ ธ๋(Text Node) |
---|
ํ ์คํธ ๋ ธ๋๋ HTML ์์์ ํ ์คํธ๋ฅผ 1ํํํ๋ค. ํ ์คํธ ๋ ธ๋๋ ์์ ๋ ธ๋์ ์์์ด๋ฉฐ ์์ ์ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ง ์ ์๋ค. ์ฆ, ํ ์คํธ ๋ ธ๋๋ DOM tree์ ์ต์ข ๋จ์ด๋ค. |
DOM์ ํตํด ์นํ์ด์ง๋ฅผ ์กฐ์(manipulate)ํ๊ธฐ ์ํด์๋ ๋ค์๊ณผ ๊ฐ์ ์์์ด ํ์ํ๋ค.
- ์กฐ์ํ๊ณ ์ํ๋ ์์๋ฅผ ์ ํ ๋๋ ํ์ํ๋ค.
- ์ ํ๋ ์์์ ์ฝํ ์ธ ๋๋ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์กฐ์ํ๋ค.
์๋ฐ์คํฌ๋ฆฝํธ๋ ์ด๊ฒ์ ํ์ํ ์๋จ(API)์ ์ ๊ณตํ๋ค.
DOM Query / Traversing (์์์์ ์ ๊ทผ)
ํ๋์ ์์ ๋ ธ๋ ์ ํ(DOM Query)
document.getElementById(id)
- id ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ผ๋ก ์์ ๋ ธ๋๋ฅผ ํ ๊ฐ ์ ํํ๋ค. ๋ณต์๊ฐ๊ฐ ์ ํ๋ ๊ฒฝ์ฐ, ์ฒซ๋ฒ์งธ ์์๋ง ๋ฐํํ๋ค.
- Return: HTMLElement๋ฅผ ์์๋ฐ์ ๊ฐ์ฒด
- ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์
// id๋ก ํ๋์ ์์๋ฅผ ์ ํํ๋ค.
const elem = document.getElementById('one');
// ํด๋์ค ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
elem.className = 'blue';
// ๊ทธ๋ฆผ: DOM tree์ ๊ฐ์ฒด ๊ตฌ์ฑ ์ฐธ๊ณ
console.log(elem); // <li id="one" class="blue">Seoul</li>
console.log(elem.__proto__); // HTMLLIElement
console.log(elem.__proto__.__proto__); // HTMLElement
console.log(elem.__proto__.__proto__.__proto__); // Element
console.log(elem.__proto__.__proto__.__proto__.__proto__); // Node
document.querySelector(cssSelector)
- CSS ์ ๋ ํฐ๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ ธ๋๋ฅผ ํ ๊ฐ ์ ํํ๋ค. ๋ณต์๊ฐ๊ฐ ์ ํ๋ ๊ฒฝ์ฐ, ์ฒซ๋ฒ์งธ ์์๋ง ๋ฐํํ๋ค.
- Return: HTMLElement๋ฅผ ์์๋ฐ์ ๊ฐ์ฒด
- IE8 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์
// HTMLCollection์ ๋ฐํํ๋ค. HTMLCollection์ liveํ๋ค.
const elems = document.getElementsByClassName('red');
for (let i = 0; i < elems.length; i++) {
// ํด๋์ค ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
elems[i].className = 'blue';
}
์ ์์ ๋ฅผ ์คํํด ๋ณด๋ฉด ์์๋๋ก ๋์ํ์ง ์๋๋ค. (๋๋ฒ์งธ ์์๋ง ํด๋์ค ๋ณ๊ฒฝ์ด ๋์ง ์๋๋ค.)
getElementsByClassName ๋ฉ์๋์ ๋ฐํ๊ฐ์ HTMLCollection์ด๋ค. ์ด๊ฒ์ ๋ฐํ๊ฐ์ด ๋ณต์์ธ ๊ฒฝ์ฐ, HTMLElement์ ๋ฆฌ์คํธ๋ฅผ ๋ด์ ๋ฐํํ๊ธฐ ์ํ ๊ฐ์ฒด๋ก ๋ฐฐ์ด๊ณผ ๋น์ทํ ์ฌ์ฉ๋ฒ์ ๊ฐ์ง๊ณ ์์ง๋ง ๋ฐฐ์ด์ ์๋ ์ ์ฌ๋ฐฐ์ด(array-like object)์ด๋ค. ๋ํ HTMLCollection์ ์ค์๊ฐ์ผ๋ก Node์ ์ํ ๋ณ๊ฒฝ์ ๋ฐ์ํ๋ค. (live HTMLCollection)
์ ์์ ๊ฐ ์์๋๋ก ๋์ํ์ง ์์ ์ด์ ๋ฅผ ์์๋ณด์.
elems.length๋ 3์ด๋ฏ๋ก 3๋ฒ์ loop๊ฐ ์คํ๋๋ค.
- i๊ฐ 0์ผ๋, elems์ ์ฒซ ์์(li#one.red)์ class ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ด className ํ๋กํผํฐ์ ์ํด red์์ blue๋ก ๋ณ๊ฒฝ๋๋ค. ์ด๋ elems๋ ์ค์๊ฐ์ผ๋ก Node์ ์ํ ๋ณ๊ฒฝ์ ๋ฐ์ํ๋ HTMLCollection ๊ฐ์ฒด์ด๋ค. elems์ ์ฒซ ์์๋ li#one.red์์ li#one.blue๋ก ๋ณ๊ฒฝ๋์์ผ๋ฏ๋ก getElementsByClassName ๋ฉ์๋์ ์ธ์๋ก ์ง์ ํ ์ ํ ์กฐ๊ฑด(โredโ)๊ณผ ๋์ด์ ๋ถํฉํ์ง ์๊ฒ ๋์ด ๋ฐํ๊ฐ์์ ์ค์๊ฐ์ผ๋ก ์ ๊ฑฐ๋๋ค.
- i๊ฐ 1์ผ๋, elems์์ ์ฒซ์งธ ์์๋ ์ ๊ฑฐ๋์์ผ๋ฏ๋ก elems[1]์ 3๋ฒ์งธ ์์(li#three.red)๊ฐ ๋๋ค. li#three.red์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ด blue๋ก ๋ณ๊ฒฝ๋๊ณ ๋ง์ฐฌ๊ฐ์ง๋ก HTMLCollection์์ ์ ์ธ๋๋ค.
- i๊ฐ 2์ผ๋, HTMLCollection์ 1,3๋ฒ์งธ ์์๊ฐ ์ค์๊ฐ์ผ๋ก ์ ๊ฑฐ๋์์ผ๋ฏ๋ก 2๋ฒ์งธ ์์(li#two.red)๋ง ๋จ์๋ค. ์ด๋ elems.length๋ 1์ด๋ฏ๋ก for ๋ฌธ์ ์กฐ๊ฑด์ i < elems.length๊ฐ false๋ก ํ๊ฐ๋์ด ๋ฐ๋ณต์ ์ข ๋ฃํ๋ค. ๋ฐ๋ผ์ elems์ ๋จ์ ์๋ 2๋ฒ์งธ li ์์(li#two.red)์ class ๊ฐ์ ๋ณ๊ฒฝ๋์ง ์๋๋ค.
์ด์ฒ๋ผ HTMLCollection๋ ์ค์๊ฐ์ผ๋ก Node์ ์ํ ๋ณ๊ฒฝ์ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ loop๊ฐ ํ์ํ ๊ฒฝ์ฐ ์ฃผ์๊ฐ ํ์ํ๋ค. ์๋์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ํํผํ ์ ์๋ค.
- ๋ฐ๋ณต๋ฌธ์ ์ญ๋ฐฉํฅ์ผ๋ก ๋๋ฆฐ๋ค.
const elems = document.getElementsByClassName('red');
for (let i = elems.length - 1; i >= 0; i--) {
elems[i].className = 'blue';
}
- while ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ๋ค. ์ด๋ elems์ ์์๊ฐ ๋จ์ ์์ง ์์ ๋๊น์ง ๋ฌดํ๋ฐ๋ณตํ๊ธฐ ์ํด index๋ 0์ผ๋ก ๊ณ ์ ์ํจ๋ค.
const elems = document.getElementsByClassName('red');
let i = 0;
while (elems.length > i) { // elems์ ์์๊ฐ ๋จ์ ์์ง ์์ ๋๊น์ง ๋ฌดํ๋ฐ๋ณต
elems[i].className = 'blue';
// i++;
}
- HTMLCollection์ ๋ฐฐ์ด๋ก ๋ณ๊ฒฝํ๋ค. ์ด ๋ฐฉ๋ฒ์ ๊ถ์ฅํ๋ค.
const elems = document.getElementsByClassName('red');
// ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ธ HTMLCollection์ ๋ฐฐ์ด๋ก ๋ณํํ๋ค.
// ๋ฐฐ์ด๋ก ๋ณํ๋ HTMLCollection์ ๋ ์ด์ liveํ์ง ์๋ค.
console.log([...elems]); // [li#one.red, li#two.red, li#three.red]
[...elems].forEach(elem => elem.className = 'blue');
- querySelectorAll ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ HTMLCollection(live)์ด ์๋ NodeList(non-live)๋ฅผ ๋ฐํํ๊ฒ ํ๋ค.
// querySelectorAll๋ Nodelist(non-live)๋ฅผ ๋ฐํํ๋ค. IE8+
const elems = document.querySelectorAll('.red');
[...elems].forEach(elem => elem.className = 'blue');
document.getElementsByTagName(tagName)
ํ๊ทธ๋ช ์ผ๋ก ์์ ๋ ธ๋๋ฅผ ๋ชจ๋ ์ ํํ๋ค.
Return: HTMLCollection (live)
๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์
// HTMLCollection์ ๋ฐํํ๋ค.
const elems = document.getElementsByTagName('li');
[...elems].forEach(elem => elem.className = 'blue');
document.querySelectorAll(selector)
- ์ง์ ๋ CSS ์ ํ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ ธ๋๋ฅผ ๋ชจ๋ ์ ํํ๋ค.
- Return: NodeList (non-live)
- IE8 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์
// Nodelist๋ฅผ ๋ฐํํ๋ค.
const elems = document.querySelectorAll('li.red');
[...elems].forEach(elem => elem.className = 'blue');
DOM Traversing
- ๋ถ๋ชจ ๋ ธ๋๋ฅผ ํ์ํ๋ค.
- Return: HTMLElement๋ฅผ ์์๋ฐ์ ๊ฐ์ฒด
- ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์
const elem = document.querySelector('#two');
elem.parentNode.className = 'blue';
- ์์ ๋ ธ๋๋ฅผ ํ์ํ๋ค.
- Return: HTMLElement๋ฅผ ์์๋ฐ์ ๊ฐ์ฒด
- IE9 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์
const elem = document.querySelector('ul');
// first Child
elem.firstChild.className = 'blue';
// last Child
elem.lastChild.className = 'blue';
์ ์์ ๋ฅผ ์คํํด ๋ณด๋ฉด ์์๋๋ก ๋์ํ์ง ์๋๋ค. ๊ทธ ์ด์ ๋ IE๋ฅผ ์ ์ธํ ๋๋ถ๋ถ์ ๋ธ๋ผ์ฐ์ ๋ค์ ์์ ์ฌ์ด์ ๊ณต๋ฐฑ ๋๋ ์ค๋ฐ๊ฟ ๋ฌธ์๋ฅผ ํ ์คํธ ๋ ธ๋๋ก ์ทจ๊ธํ๊ธฐ ๋๋ฌธ์ด๋ค. ์ด๊ฒ์ ํํผํ๊ธฐ ์ํด์๋ ์๋์ ๊ฐ์ด HTML์ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ๊ฑฐ๋ jQuery: .prev()์ jQuery: .next()๋ฅผ ์ฌ์ฉํ๋ค.
<ul><li
id='one' class='red'>Seoul</li><li
id='two' class='red'>London</li><li
id='three' class='red'>Newyork</li><li
id='four'>Tokyo</li></ul>
๋๋ firstElementChild, lastElementChild๋ฅผ ์ฌ์ฉํ ์๋ ์๋ค. ์ด ๋๊ฐ์ง ํ๋กํผํฐ๋ ๋ชจ๋ IE9 ์ด์์์ ์ ์ ๋์ํ๋ค.
const elem = document.querySelector('ul');
// first Child
elem.firstElementChild.className = 'blue';
// last Child
elem.lastElementChild.className = 'blue';
hasChildNodes() |
---|
์์ ๋
ธ๋๊ฐ ์๋์ง ํ์ธํ๊ณ Boolean ๊ฐ์ ๋ฐํํ๋ค. Return: Boolean ๊ฐ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ |
childNodes |
---|
์์ ๋
ธ๋์ ์ปฌ๋ ์
์ ๋ฐํํ๋ค. ํ
์คํธ ์์๋ฅผ ํฌํจํ ๋ชจ๋ ์์ ์์๋ฅผ ๋ฐํํ๋ค. Return: NodeList (non-live) ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ |
children |
---|
์์ ๋
ธ๋์ ์ปฌ๋ ์
์ ๋ฐํํ๋ค. ์์ ์์ ์ค์์ Element type ์์๋ง์ ๋ฐํํ๋ค. Return: HTMLCollection (live) IE9 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์ |
const elem = document.querySelector('ul');
if (elem.hasChildNodes()) {
console.log(elem.childNodes);
// ํ
์คํธ ์์๋ฅผ ํฌํจํ ๋ชจ๋ ์์ ์์๋ฅผ ๋ฐํํ๋ค.
// NodeList(9) [text, li#one.red, text, li#two.red, text, li#three.red, text, li#four, text]
console.log(elem.children);
// ์์ ์์ ์ค์์ Element type ์์๋ง์ ๋ฐํํ๋ค.
// HTMLCollection(4) [li#one.red, li#two.red, li#three.red, li#four, one: li#one.red, two: li#two.red, three: li#three.red, four: li#four]
[...elem.children].forEach(el => console.log(el.nodeType)); // 1 (=> Element node)
}
previousSibling, nextSibling |
---|
ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค. text node๋ฅผ ํฌํจํ ๋ชจ๋ ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค. Return: HTMLElement๋ฅผ ์์๋ฐ์ ๊ฐ์ฒด ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ |
previousElementSibling, nextElementSibling |
---|
ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค. ํ์ ๋
ธ๋ ์ค์์ Element type ์์๋ง์ ํ์ํ๋ค. Return: HTMLElement๋ฅผ ์์๋ฐ์ ๊ฐ์ฒด IE9 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์ |
DOM Manipulation(์กฐ์)
ํ ์คํธ ๋ ธ๋์์ ์ ๊ทผ/์์
์์์ ํ ์คํธ๋ ํ ์คํธ ๋ ธ๋์ ์ ์ฅ๋์ด ์๋ค. ํ ์คํธ ๋ ธ๋์ ์ ๊ทผํ๋ ค๋ฉด ์๋์ ๊ฐ์ ์์์ด ํ์ํ๋ค.
- ํด๋น ํ ์คํธ ๋ ธ๋์ ๋ถ๋ชจ ๋ ธ๋๋ฅผ ์ ํํ๋ค. ํ ์คํธ ๋ ธ๋๋ ์์ ๋ ธ๋์ ์์์ด๋ค.
- firstChild ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ ์คํธ ๋ ธ๋๋ฅผ ํ์ํ๋ค.
- ํ
์คํธ ๋
ธ๋์ ์ ์ผํ ํ๋กํผํฐ(
nodeValue
)๋ฅผ ์ด์ฉํ์ฌ ํ ์คํธ๋ฅผ ์ทจ๋ํ๋ค. nodeValue
๋ฅผ ์ด์ฉํ์ฌ ํ ์คํธ๋ฅผ ์์ ํ๋ค.
nodeValue |
---|
๋
ธ๋์ ๊ฐ์ ๋ฐํํ๋ค. Return: ํ ์คํธ ๋ ธ๋์ ๊ฒฝ์ฐ๋ ๋ฌธ์์ด, ์์ ๋ ธ๋์ ๊ฒฝ์ฐ null ๋ฐํ IE6 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
nodeName, nodeType์ ํตํด ๋ ธ๋์ ์ ๋ณด๋ฅผ ์ทจ๋ํ ์ ์๋ค.
// ํด๋น ํ
์คํธ ๋
ธ๋์ ๋ถ๋ชจ ์์ ๋
ธ๋๋ฅผ ์ ํํ๋ค.
const one = document.getElementById('one');
console.dir(one); // HTMLLIElement: li#one.red
// nodeName, nodeType์ ํตํด ๋
ธ๋์ ์ ๋ณด๋ฅผ ์ทจ๋ํ ์ ์๋ค.
console.log(one.nodeName); // LI
console.log(one.nodeType); // 1: Element node
// firstChild ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ
์คํธ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
const textNode = one.firstChild;
// nodeName, nodeType์ ํตํด ๋
ธ๋์ ์ ๋ณด๋ฅผ ์ทจ๋ํ ์ ์๋ค.
console.log(textNode.nodeName); // #text
console.log(textNode.nodeType); // 3: Text node
// nodeValue ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋
ธ๋์ ๊ฐ์ ์ทจ๋ํ๋ค.
console.log(textNode.nodeValue); // Seoul
// nodeValue ํ๋กํผํฐ๋ฅผ ์ด์ฉํ์ฌ ํ
์คํธ๋ฅผ ์์ ํ๋ค.
textNode.nodeValue = 'Pusan';
์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋์ ์ ๊ทผ/์์
์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋์ ์กฐ์ํ ๋ ๋ค์ ํ๋กํผํฐ ๋๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
className |
---|
class ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ์ทจ๋ ๋๋ ๋ณ๊ฒฝํ๋ค. className ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ๋ ๊ฒฝ์ฐ, class ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด class ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์์ฑํ๊ณ ์ง์ ๋ ๊ฐ์ ์ค์ ํ๋ค. class ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ด ์ฌ๋ฌ ๊ฐ์ผ ๊ฒฝ์ฐ, ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋ ๋ฌธ์์ด์ด ๋ฐํ๋๋ฏ๋ก String ๋ฉ์๋ split(' ') ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด๋ก ๋ณ๊ฒฝํ์ฌ ์ฌ์ฉํ๋ค. ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
classList |
---|
add, remove, item, toggle, contains, replace ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค. IE10 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
const elems = document.querySelectorAll('li');
// className
[...elems].forEach(elem => {
// class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋ํ์ฌ ํ์ธ
if (elem.className === 'red') {
// class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
elem.className = 'blue';
}
});
// classList
[...elems].forEach(elem => {
// class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ ํ์ธ
if (elem.classList.contains('blue')) {
// class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ ๋ณ๊ฒฝํ๋ค.
elem.classList.replace('blue', 'red');
}
});
id |
---|
id ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ์ทจ๋ ๋๋ ๋ณ๊ฒฝํ๋ค. id ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ๋ ๊ฒฝ์ฐ, id ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด id ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์์ฑํ๊ณ ์ง์ ๋ ๊ฐ์ ์ค์ ํ๋ค. ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
// h1 ํ๊ทธ ์์ ์ค ์ฒซ๋ฒ์งธ ์์๋ฅผ ์ทจ๋
const heading = document.querySelector('h1');
console.dir(heading); // HTMLHeadingElement: h1
console.log(heading.firstChild.nodeValue); // Cities
// id ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ๋ณ๊ฒฝ.
// id ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด id ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์์ฑํ๊ณ ์ง์ ๋ ๊ฐ์ ์ค์
heading.id = 'heading';
console.log(heading.id); // heading
hasAttribute(attribute) |
---|
์ง์ ํ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ๊ฐ์ง๊ณ ์๋์ง ๊ฒ์ฌํ๋ค. Return : Boolean IE8 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
getAttribute(attribute) |
---|
์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ์ทจ๋ํ๋ค. Return : ๋ฌธ์์ด ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
setAttribute(attribute, value) |
---|
์ดํธ๋ฆฌ๋ทฐํธ์ ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ค์ ํ๋ค. Return : undefined ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
removeAttribute(attribute) |
---|
์ง์ ํ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ ๊ฑฐํ๋ค. Return : undefined ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
<!DOCTYPE html>
<html>
<body>
<input type="text">
<script>
const input = document.querySelector('input[type=text]');
console.log(input);
// value ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด
if (!input.hasAttribute('value')) {
// value ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ถ๊ฐํ๊ณ ๊ฐ์ผ๋ก 'hello!'๋ฅผ ์ค์
input.setAttribute('value', 'hello!');
}
// value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋
console.log(input.getAttribute('value')); // hello!
// value ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ ๊ฑฐ
input.removeAttribute('value');
// value ์ดํธ๋ฆฌ๋ทฐํธ์ ์กด์ฌ๋ฅผ ํ์ธ
console.log(input.hasAttribute('value')); // false
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input class="password" type="password" value="123">
<button class="show">show</button>
<script>
const $password = document.querySelector('.password');
const $show = document.querySelector('.show');
function makeClickHandler() {
let isShow = false;
return function () {
$password.setAttribute('type', isShow ? 'password' : 'text');
isShow = !isShow;
$show.innerHTML = isShow ? 'hide' : 'show';
};
}
$show.onclick = makeClickHandler();
</script>
</body>
</html>
HTML ์ปจํ ์ธ ์กฐ์(Manipulation)
HTML ์ฝํ ์ธ ๋ฅผ ์กฐ์(Manipulation)ํ๊ธฐ ์ํด ์๋์ ํ๋กํผํฐ ๋๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ๋งํฌ์ ์ด ํฌํจ๋ ์ฝํ ์ธ ๋ฅผ ์ถ๊ฐํ๋ ํ์๋ ํฌ๋ก์ค ์คํฌ๋ฆฝํ ๊ณต๊ฒฉ(XSS: Cross-Site Scripting Attacks)์ ์ทจ์ฝํ๋ฏ๋ก ์ฃผ์๊ฐ ํ์ํ๋ค.
textContent |
---|
์์์ ํ
์คํธ ์ฝํ
์ธ ๋ฅผ ์ทจ๋ ๋๋ ๋ณ๊ฒฝํ๋ค. ์ด๋ ๋งํฌ์
์ ๋ฌด์๋๋ค. textContent๋ฅผ ํตํด ์์์ ์๋ก์ด ํ
์คํธ๋ฅผ ํ ๋นํ๋ฉด ํ
์คํธ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค. ์ด๋ ์์ํ ํ
์คํธ๋ง ์ง์ ํด์ผ ํ๋ฉฐ ๋งํฌ์
์ ํฌํจ์ํค๋ฉด ๋ฌธ์์ด๋ก ์ธ์๋์ด ๊ทธ๋๋ก ์ถ๋ ฅ๋๋ค. IE9 ์ด์์ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
<!DOCTYPE html>
<html>
<head>
<style>
.red { color: #ff0000; }
.blue { color: #0000ff; }
</style>
</head>
<body>
<div>
<h1>Cities</h1>
<ul>
<li id="one" class="red">Seoul</li>
<li id="two" class="red">London</li>
<li id="three" class="red">Newyork</li>
<li id="four">Tokyo</li>
</ul>
</div>
<script>
const ul = document.querySelector('ul');
// ์์์ ํ
์คํธ ์ทจ๋
console.log(ul.textContent);
/*
Seoul
London
Newyork
Tokyo
*/
const one = document.getElementById('one');
// ์์์ ํ
์คํธ ์ทจ๋
console.log(one.textContent); // Seoul
// ์์์ ํ
์คํธ ๋ณ๊ฒฝ
one.textContent += ', Korea';
console.log(one.textContent); // Seoul, Korea
// ์์์ ๋งํฌ์
์ด ํฌํจ๋ ์ฝํ
์ธ ๋ณ๊ฒฝ.
one.textContent = '<h1>Heading</h1>';
// ๋งํฌ์
์ด ๋ฌธ์์ด๋ก ํ์๋๋ค.
console.log(one.textContent); // <h1>Heading</h1>
</script>
</body>
</html>
innerText |
---|
innerText ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ๋ ์์์ ํ
์คํธ ์ฝํ
์ธ ์๋ง ์ ๊ทผํ ์ ์๋ค. ํ์ง๋ง ์๋์ ์ด์ ๋ก ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข๋ค ๋นํ์ค์ด๋ค. CSS์ ์์ข ์ ์ด๋ค. ์๋ฅผ ๋ค์ด CSS์ ์ํด ๋นํ์(visibility: hidden;)๋ก ์ง์ ๋์ด ์๋ค๋ฉด ํ ์คํธ๊ฐ ๋ฐํ๋์ง ์๋๋ค. CSS๋ฅผ ๊ณ ๋ คํด์ผ ํ๋ฏ๋ก textContent ํ๋กํผํฐ๋ณด๋ค ๋๋ฆฌ๋ค. |
innerHTML |
---|
ํด๋น ์์์ ๋ชจ๋ ์์ ์์๋ฅผ ํฌํจํ๋ ๋ชจ๋ ์ฝํ ์ธ ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ์ทจ๋ํ ์ ์๋ค. ์ด ๋ฌธ์์ด์ ๋งํฌ์ ์ ํฌํจํ๋ค. |
const ul = document.querySelector('ul');
// innerHTML ํ๋กํผํฐ๋ ๋ชจ๋ ์์ ์์๋ฅผ ํฌํจํ๋ ๋ชจ๋ ์ฝํ
์ธ ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ์ทจ๋ํ ์ ์๋ค. ์ด ๋ฌธ์์ด์ ๋งํฌ์
์ ํฌํจํ๋ค.
console.log(ul.innerHTML);
// IE๋ฅผ ์ ์ธํ ๋๋ถ๋ถ์ ๋ธ๋ผ์ฐ์ ๋ค์ ์์ ์ฌ์ด์ ๊ณต๋ฐฑ ๋๋ ์ค๋ฐ๊ฟ ๋ฌธ์๋ฅผ ํ
์คํธ ๋
ธ๋๋ก ์ทจ๊ธํ๋ค
/*
<li id="one" class="red">Seoul</li>
<li id="two" class="red">London</li>
<li id="three" class="red">Newyork</li>
<li id="four">Tokyo</li>
*/
innerHTML ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋งํฌ์ ์ด ํฌํจ๋ ์๋ก์ด ์ฝํ ์ธ ๋ฅผ ์ง์ ํ๋ฉด ์๋ก์ด ์์๋ฅผ DOM์ ์ถ๊ฐํ ์ ์๋ค.
const one = document.getElementById('one');
// ๋งํฌ์
์ด ํฌํจ๋ ์ฝํ
์ธ ์ทจ๋
console.log(one.innerHTML); // Seoul
// ๋งํฌ์
์ด ํฌํจ๋ ์ฝํ
์ธ ๋ณ๊ฒฝ
one.innerHTML += '<em class="blue">, Korea</em>';
// ๋งํฌ์
์ด ํฌํจ๋ ์ฝํ
์ธ ์ทจ๋
console.log(one.innerHTML); // Seoul <em class="blue">, Korea</em>
์์ ๊ฐ์ด ๋งํฌ์ ์ด ํฌํจ๋ ์ฝํ ์ธ ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ ํฌ๋ก์ค ์คํฌ๋ฆฝํ ๊ณต๊ฒฉ(XSS: Cross-Site Scripting Attacks)์ ์ทจ์ฝํ๋ค.
// ํฌ๋ก์ค ์คํฌ๋ฆฝํ
๊ณต๊ฒฉ ์ฌ๋ก
// ์คํฌ๋ฆฝํธ ํ๊ทธ๋ฅผ ์ถ๊ฐํ์ฌ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์คํ๋๋๋ก ํ๋ค.
// HTML5์์ innerHTML๋ก ์ฝ์
๋ <script> ์ฝ๋๋ ์คํ๋์ง ์๋๋ค.
// ํฌ๋กฌ, ํ์ด์ดํญ์ค ๋ฑ์ ๋ธ๋ผ์ฐ์ ๋ ์ต์ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์๋ ์๋ํ์ง ์์ ์๋ ์๋ค.
elem.innerHTML = '<script>alert("XSS!")</script>';
// ์๋ฌ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์์ผ ์คํฌ๋ฆฝํธ๊ฐ ์คํ๋๋๋ก ํ๋ค.
// ํฌ๋กฌ์์๋ ์คํ๋๋ค!
elem.innerHTML = '<img src="#" onerror="alert(\'XSS\')">';
DOM ์กฐ์ ๋ฐฉ์
innerHTML ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ์๋ก์ด ์ฝํ ์ธ ๋ฅผ ์ถ๊ฐํ ์ ์๋ ๋ฐฉ๋ฒ์ DOM์ ์ง์ ์กฐ์ํ๋ ๊ฒ์ด๋ค. ํ ๊ฐ์ ์์๋ฅผ ์ถ๊ฐํ๋ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค. ์ด ๋ฐฉ๋ฒ์ ๋ค์์ ์์์ ๋ฐ๋ผ ์งํํ๋ค.
- ์์ ๋ ธ๋ ์์ฑ createElement() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก์ด ์์ ๋ ธ๋๋ฅผ ์์ฑํ๋ค. createElement() ๋ฉ์๋์ ์ธ์๋ก ํ๊ทธ ์ด๋ฆ์ ์ ๋ฌํ๋ค.
- ํ ์คํธ ๋ ธ๋ ์์ฑ createTextNode() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก์ด ํ ์คํธ ๋ ธ๋๋ฅผ ์์ฑํ๋ค. ๊ฒฝ์ฐ์ ๋ฐ๋ผ ์๋ต๋ ์ ์์ง๋ง ์๋ตํ๋ ๊ฒฝ์ฐ, ์ฝํ ์ธ ๊ฐ ๋น์ด ์๋ ์์๊ฐ ๋๋ค.
- ์์ฑ๋ ์์๋ฅผ DOM์ ์ถ๊ฐ appendChild() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ๋ ๋ ธ๋๋ฅผ DOM tree์ ์ถ๊ฐํ๋ค. ๋๋ removeChild() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ DOM tree์์ ๋ ธ๋๋ฅผ ์ญ์ ํ ์๋ ์๋ค.
createElement(tagName) |
---|
ํ๊ทธ์ด๋ฆ์ ์ธ์๋ก ์ ๋ฌํ์ฌ ์์๋ฅผ ์์ฑํ๋ค. Return: HTMLElement๋ฅผ ์์๋ฐ์ ๊ฐ์ฒด ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
createTextNode(text) |
---|
ํ
์คํธ๋ฅผ ์ธ์๋ก ์ ๋ฌํ์ฌ ํ
์คํธ ๋
ธ๋๋ฅผ ์์ฑํ๋ค. Return: Text ๊ฐ์ฒด ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
appendChild(Node) |
---|
์ธ์๋ก ์ ๋ฌํ ๋
ธ๋๋ฅผ ๋ง์ง๋ง ์์ ์์๋ก DOM ํธ๋ฆฌ์ ์ถ๊ฐํ๋ค. Return: ์ถ๊ฐํ ๋ ธ๋ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
removeChild(Node) |
---|
์ธ์๋ก ์ ๋ฌํ ๋
ธ๋๋ฅผ DOM ํธ๋ฆฌ์ ์ ๊ฑฐํ๋ค. Return: ์ถ๊ฐํ ๋ ธ๋ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
// ํ๊ทธ์ด๋ฆ์ ์ธ์๋ก ์ ๋ฌํ์ฌ ์์๋ฅผ ์์ฑ
const newElem = document.createElement('li');
// const newElem = document.createElement('<li>test</li>');
// Uncaught DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('<li>test</li>') is not a valid name.
// ํ
์คํธ ๋
ธ๋๋ฅผ ์์ฑ
const newText = document.createTextNode('Beijing');
// ํ
์คํธ ๋
ธ๋๋ฅผ newElem ์์์ผ๋ก DOM ํธ๋ฆฌ์ ์ถ๊ฐ
newElem.appendChild(newText);
const container = document.querySelector('ul');
// newElem์ container์ ์์์ผ๋ก DOM ํธ๋ฆฌ์ ์ถ๊ฐ. ๋ง์ง๋ง ์์๋ก ์ถ๊ฐ๋๋ค.
container.appendChild(newElem);
const removeElem = document.getElementById('one');
// container์ ์์์ธ removeElem ์์๋ฅผ DOM ํธ๋ฆฌ์ ์ ๊ฑฐํ๋ค.
container.removeChild(removeElem);
insertAdjacentHTML()
insertAdjacentHTML(position, string) |
---|
์ธ์๋ก ์ ๋ฌํ ํ
์คํธ๋ฅผ HTML๋ก ํ์ฑํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ก ์์ฑ๋ ๋
ธ๋๋ฅผ DOM ํธ๋ฆฌ์ ์ง์ ๋ ์์น์ ์ฝ์
ํ๋ค. ์ฒซ๋ฒ์งธ ์ธ์๋ ์ฝ์
์์น, ๋๋ฒ์งธ ์ธ์๋ ์ฝ์
ํ ์์๋ฅผ ํํํ ๋ฌธ์์ด์ด๋ค. ์ฒซ๋ฒ์งธ ์ธ์๋ก ์ฌ ์ ์๋ ๊ฐ์ ์๋์ ๊ฐ๋ค. โbeforebeginโ โafterbeginโ โbeforeendโ โafterendโ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์ํ๋ค. |
โ insertAdjacentHTML ๋ฉ์๋์ position parameter
const one = document.getElementById('one');
// ๋งํฌ์
์ด ํฌํจ๋ ์์ ์ถ๊ฐ
one.insertAdjacentHTML('beforeend', '<em class="blue">, Korea</em>');
innerHTML vs DOM์กฐ์๋ฐฉ์ vs insertAdjacentHTML
innerHTML
์ฅ์ | ๋จ์ |
---|---|
DOM ์กฐ์ ๋ฐฉ์์ ๋นํด ๋น ๋ฅด๊ณ ๊ฐํธํ๋ค. | XSS๊ณต๊ฒฉ์ ์ทจ์ฝ์ ์ด ์๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์๋ก ๋ถํฐ ์ ๋ ฅ๋ฐ์ ์ฝํ ์ธ (untrusted data: ๋๊ธ, ์ฌ์ฉ์ ์ด๋ฆ ๋ฑ)๋ฅผ ์ถ๊ฐํ ๋ ์ฃผ์ํ์ฌ์ผ ํ๋ค. |
๊ฐํธํ๊ฒ ๋ฌธ์์ด๋ก ์ ์ํ ์ฌ๋ฌ ์์๋ฅผ DOM์ ์ถ๊ฐํ ์ ์๋ค. | ํด๋น ์์์ ๋ด์ฉ์ ๋ฎ์ด ์ด๋ค. ์ฆ, HTML์ ๋ค์ ํ์ฑํ๋ค. ์ด๊ฒ์ ๋นํจ์จ์ ์ด๋ค. |
์ฝํ ์ธ ๋ฅผ ์ทจ๋ํ ์ ์๋ค. | ย |
DOM ์กฐ์ ๋ฐฉ์
์ฅ์ | ๋จ์ |
---|---|
ํน์ ๋ ธ๋ ํ ๊ฐ(๋ ธ๋, ํ ์คํธ, ๋ฐ์ดํฐ ๋ฑ)๋ฅผ DOM์ ์ถ๊ฐํ ๋ ์ ํฉํ๋ค. | innerHTML๋ณด๋ค ๋๋ฆฌ๊ณ ๋ ๋ง์ ์ฝ๋๊ฐ ํ์ํ๋ค. |
insertAdjacentHTML()
์ฅ์ | ๋จ์ |
---|---|
๊ฐํธํ๊ฒ ๋ฌธ์์ด๋ก ์ ์๋ ์ฌ๋ฌ ์์๋ฅผ DOM์ ์ถ๊ฐํ ์ ์๋ค. | XSS๊ณต๊ฒฉ์ ์ทจ์ฝ์ ์ด ์๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์๋ก ๋ถํฐ ์ ๋ ฅ๋ฐ์ ์ฝํ ์ธ (untrusted data: ๋๊ธ, ์ฌ์ฉ์ ์ด๋ฆ ๋ฑ)๋ฅผ ์ถ๊ฐํ ๋ ์ฃผ์ํ์ฌ์ผ ํ๋ค. |
์ฝ์ ๋๋ ์์น๋ฅผ ์ ์ ํ ์ ์๋ค. | ย |
๊ฒฐ๋ก
innerHTML๊ณผ insertAdjacentHTML()์ ํฌ๋ก์ค ์คํฌ๋ฆฝํ ๊ณต๊ฒฉ(XSS: Cross-Site Scripting Attacks)์ ์ทจ์ฝํ๋ค. ๋ฐ๋ผ์ untrusted data์ ๊ฒฝ์ฐ, ์ฃผ์ํ์ฌ์ผ ํ๋ค. ํ ์คํธ๋ฅผ ์ถ๊ฐ ๋๋ ๋ณ๊ฒฝ์์๋ textContent, ์๋ก์ด ์์์ ์ถ๊ฐ ๋๋ ์ญ์ ์์๋ DOM ์กฐ์ ๋ฐฉ์์ ์ฌ์ฉํ๋๋ก ํ๋ค.
style
style ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ฉด inline ์คํ์ผ ์ ์ธ์ ์์ฑํ๋ค. ํน์ ์์์ inline ์คํ์ผ์ ์ง์ ํ๋ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค.
const four = document.getElementById('four');
// inline ์คํ์ผ ์ ์ธ์ ์์ฑ
four.style.color = 'blue';
// font-size์ ๊ฐ์ด '-'์ผ๋ก ๊ตฌ๋ถ๋๋ ํ๋กํผํฐ๋ ์นด๋ฉ์ผ์ด์ค๋ก ๋ณํํ์ฌ ์ฌ์ฉํ๋ค.
four.style.fontSize = '2em';
style ํ๋กํผํฐ์ ๊ฐ์ ์ทจ๋ํ๋ ค๋ฉด window.getComputedStyle์ ์ฌ์ฉํ๋ค. window.getComputedStyle ๋ฉ์๋๋ ์ธ์๋ก ์ฃผ์ด์ง ์์์ ๋ชจ๋ CSS ํ๋กํผํฐ ๊ฐ์ ๋ฐํํ๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>style ํ๋กํผํฐ ๊ฐ ์ทจ๋</title>
<style>
.box {
width: 100px;
height: 50px;
background-color: red;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
const box = document.querySelector('.box');
const width = getStyle(box, 'width');
const height = getStyle(box, 'height');
const backgroundColor = getStyle(box, 'background-color');
const border = getStyle(box, 'border');
console.log('width: ' + width);
console.log('height: ' + height);
console.log('backgroundColor: ' + backgroundColor);
console.log('border: ' + border);
/**
* ์์์ ์ ์ฉ๋ CSS ํ๋กํผํฐ๋ฅผ ๋ฐํํ๋ค.
* @param {HTTPElement} elem - ๋์ ์์ ๋
ธ๋.
* @param {string} prop - ๋์ CSS ํ๋กํผํฐ.
* @returns {string} CSS ํ๋กํผํฐ์ ๊ฐ.
*/
function getStyle(elem, prop) {
return window.getComputedStyle(elem, null).getPropertyValue(prop);
}
</script>
</body>
</html>
[์ฐธ๊ณ ]
W3C :Document Object Model
๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ(DOM)
DOM Core: DOM methods and properties
W3C DOM Compatibility: DOM methods and properties specific to HTML elements