<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
</head>
<body>
<ul>
<li id="apple">Apple</li>
<li id="banana">Banana</li>
<li id="orange">Orange</li>
</ul>
<script src="app.js"></script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input type="text">
<script>
// input ์์ ๋
ธ๋ ๊ฐ์ฒด๋ฅผ ์ ํ
const $input = document.querySelector('input');
// input ์์ ๋
ธ๋ ๊ฐ์ฒด์ ํ๋กํ ํ์
์ฒด์ธ
console.log(
Object.getPrototypeOf($input) === HTMLInputElement.prototype,
Object.getPrototypeOf(HTMLInputElement.prototype) === HTMLElement.prototype,
Object.getPrototypeOf(HTMLElement.prototype) === Element.prototype,
Object.getPrototypeOf(Element.prototype) === Node.prototype,
Object.getPrototypeOf(Node.prototype) === EventTarget.prototype,
Object.getPrototypeOf(EventTarget.prototype) === Object.prototype
); // ๋ชจ๋ true
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul>
<li id="apple">Apple</li>
<li id="banana">Banana</li>
<li id="orange">Orange</li>
</ul>
<script>
// id ๊ฐ์ด 'banana'์ธ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค.
// ๋ ๋ฒ์งธ li ์์๊ฐ ํ์ฑ๋์ด ์์ฑ๋ ์์ ๋
ธ๋๊ฐ ๋ฐํ๋๋ค.
const $elem = document.getElementById('banana');
// ์ทจ๋ํ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
$elem.style.color = 'red';
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul>
<li id="banana">Apple</li>
<li id="banana">Banana</li>
<li id="banana">Orange</li>
</ul>
<script>
// getElementById ๋ฉ์๋๋ ์ธ์ ๋ ๋จ ํ๋์ ์์ ๋
ธ๋๋ฅผ ๋ฐํํ๋ค.
// ์ฒซ ๋ฒ์งธ li ์์๊ฐ ํ์ฑ๋์ด ์์ฑ๋ ์์ ๋
ธ๋๊ฐ ๋ฐํ๋๋ค.
const $elem = document.getElementById('banana');
// ์ทจ๋ํ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
$elem.style.color = 'red';
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul>
<li id="apple">Apple</li>
<li id="banana">Banana</li>
<li id="orange">Orange</li>
</ul>
<script>
// id ๊ฐ์ด 'grape'์ธ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค. null์ด ๋ฐํ๋๋ค.
const $elem = document.getElementById('grape');
// ์ทจ๋ํ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
$elem.style.color = 'red';
// -> TypeError: Cannot read property 'style' of null
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo"></div>
<script>
// id ๊ฐ๊ณผ ๋์ผํ ์ด๋ฆ์ ์ ์ญ ๋ณ์๊ฐ ์๋ฌต์ ์ผ๋ก ์ ์ธ๋๊ณ ํด๋น ๋
ธ๋ ๊ฐ์ฒด๊ฐ ํ ๋น๋๋ค.
console.log(foo === document.getElementById('foo')); // true
// ์๋ฌต์ ์ ์ญ์ผ๋ก ์์ฑ๋ ์ ์ญ ํ๋กํผํฐ๋ ์ญ์ ๋์ง๋ง ์ ์ญ ๋ณ์๋ ์ญ์ ๋์ง ์๋๋ค.
delete foo;
console.log(foo); // <div id="foo"></div>
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo"></div>
<script>
let foo = 1;
// id ๊ฐ๊ณผ ๋์ผํ ์ด๋ฆ์ ์ ์ญ ๋ณ์๊ฐ ์ด๋ฏธ ์ ์ธ๋์ด ์์ผ๋ฉด ๋
ธ๋ ๊ฐ์ฒด๊ฐ ์ฌํ ๋น๋์ง ์๋๋ค.
console.log(foo); // 1
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul>
<li id="apple">Apple</li>
<li id="banana">Banana</li>
<li id="orange">Orange</li>
</ul>
<script>
// ํ๊ทธ ์ด๋ฆ์ด li์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
// ํ์๋ ์์ ๋
ธ๋๋ค์ HTMLCollection ๊ฐ์ฒด์ ๋ด๊ฒจ ๋ฐํ๋๋ค.
// HTMLCollection ๊ฐ์ฒด๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฉด์ ์ดํฐ๋ฌ๋ธ์ด๋ค.
const $elems = document.getElementsByTagName('li');
// ์ทจ๋ํ ๋ชจ๋ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
// HTMLCollection ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ์ํํ๋ฉฐ color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
[...$elems].forEach(elem => { elem.style.color = 'red'; });
</script>
</body>
</html>
// ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $all = document.getElementsByTagName('*');
// -> HTMLCollection(8)ย [html, head, body, ul, li#apple, li#banana, li#orange, script, apple: li#apple, banana: li#banana, orange: li#orange]
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
<ul>
<li>HTML</li>
</ul>
<script>
// DOM ์ ์ฒด์์ ํ๊ทธ ์ด๋ฆ์ด li์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $lisFromDocument = document.getElementsByTagName('li');
console.log($lisFromDocument); // HTMLCollection(4) [li, li, li, li]
// #fruits ์์์ ์์ ๋
ธ๋ ์ค์์ ํ๊ทธ ์ด๋ฆ์ด li์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋
// ํ์ํ์ฌ ๋ฐํํ๋ค.
const $fruits = document.getElementById('fruits');
const $lisFromFruits = $fruits.getElementsByTagName('li');
console.log($lisFromFruits); // HTMLCollection(3) [li, li, li]
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul>
<li class="fruit apple">Apple</li>
<li class="fruit banana">Banana</li>
<li class="fruit orange">Orange</li>
</ul>
<script>
// class ๊ฐ์ด 'fruit'์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ HTMLCollection ๊ฐ์ฒด์ ๋ด์ ๋ฐํํ๋ค.
const $elems = document.getElementsByClassName('fruit');
// ์ทจ๋ํ ๋ชจ๋ ์์์ CSS color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
[...$elems].forEach(elem => { elem.style.color = 'red'; });
// class ๊ฐ์ด 'fruit apple'์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ HTMLCollection ๊ฐ์ฒด์ ๋ด์ ๋ฐํํ๋ค.
const $apples = document.getElementsByClassName('fruit apple');
// ์ทจ๋ํ ๋ชจ๋ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
[...$apples].forEach(elem => { elem.style.color = 'blue'; });
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
<div class="banana">Banana</div>
<script>
// DOM ์ ์ฒด์์ class ๊ฐ์ด 'banana'์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $bananasFromDocument = document.getElementsByClassName('banana');
console.log($bananasFromDocument); // HTMLCollection(2) [li.banana, div.banana]
// #fruits ์์์ ์์ ๋
ธ๋ ์ค์์ class ๊ฐ์ด 'banana'์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $fruits = document.getElementById('fruits');
const $bananasFromFruits = $fruits.getElementsByClassName('banana');
console.log($bananasFromFruits); // HTMLCollection [li.banana]
</script>
</body>
</html>
/* ์ ์ฒด ์ ํ์: ๋ชจ๋ ์์๋ฅผ ์ ํ */
* { ... }
/* ํ๊ทธ ์ ํ์: ๋ชจ๋ p ํ๊ทธ ์์๋ฅผ ๋ชจ๋ ์ ํ */
p { ... }
/* id ์ ํ์: id ๊ฐ์ด 'foo'์ธ ์์๋ฅผ ๋ชจ๋ ์ ํ */
#foo { ... }
/* class ์ ํ์: class ๊ฐ์ด 'foo'์ธ ์์๋ฅผ ๋ชจ๋ ์ ํ */
.foo { ... }
/* ์ดํธ๋ฆฌ๋ทฐํธ ์ ํ์: input ์์ ์ค์ type ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ด 'text'์ธ ์์๋ฅผ ๋ชจ๋ ์ ํ */
input[type=text] { ... }
/* ํ์ ์ ํ์: div ์์์ ํ์ ์์ ์ค p ์์๋ฅผ ๋ชจ๋ ์ ํ */
div p { ... }
/* ์์ ์ ํ์: div ์์์ ์์ ์์ ์ค p ์์๋ฅผ ๋ชจ๋ ์ ํ */
div > p { ... }
/* ์ธ์ ํ์ ์ ํ์: p ์์์ ํ์ ์์ ์ค์ p ์์ ๋ฐ๋ก ๋ค์ ์์นํ๋ ul ์์๋ฅผ ์ ํ */
p + ul { ... }
/* ์ผ๋ฐ ํ์ ์ ํ์: p ์์์ ํ์ ์์ ์ค์ p ์์ ๋ค์ ์์นํ๋ ul ์์๋ฅผ ๋ชจ๋ ์ ํ */
p ~ ul { ... }
/* ๊ฐ์ ํด๋์ค ์ ํ์: hover ์ํ์ธ a ์์๋ฅผ ๋ชจ๋ ์ ํ */
a:hover { ... }
/* ๊ฐ์ ์์ ์ ํ์: p ์์์ ์ฝํ
์ธ ์ ์์ ์์นํ๋ ๊ณต๊ฐ์ ์ ํ
์ผ๋ฐ์ ์ผ๋ก content ํ๋กํผํฐ์ ํจ๊ป ์ฌ์ฉ๋๋ค. */
p::before { ... }
<!DOCTYPE html>
<html>
<body>
<ul>
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
<script>
// class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ด 'banana'์ธ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $elem = document.querySelector('.banana');
// ์ทจ๋ํ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
$elem.style.color = 'red';
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul>
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
<script>
// ul ์์์ ์์ ์์์ธ li ์์๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $elems = document.querySelectorAll('ul > li');
// ์ทจ๋ํ ์์ ๋
ธ๋๋ค์ NodeList ๊ฐ์ฒด์ ๋ด๊ฒจ ๋ฐํ๋๋ค.
console.log($elems); // NodeList(3) [li.apple, li.banana, li.orange]
// ์ทจ๋ํ ๋ชจ๋ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
// NodeList๋ forEach ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
$elems.forEach(elem => { elem.style.color = 'red'; });
</script>
</body>
</html>
// ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $all = document.querySelectorAll('*');
// -> NodeList(8) [html, head, body, ul, li#apple, li#banana, li#orange, script]
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
<script>
const $apple = document.querySelector('.apple');
// $apple ๋
ธ๋๋ '#fruits > li.apple'๋ก ์ทจ๋ํ ์ ์๋ค.
console.log($apple.matches('#fruits > li.apple')); // true
// $apple ๋
ธ๋๋ '#fruits > li.banana'๋ก ์ทจ๋ํ ์ ์๋ค.
console.log($apple.matches('#fruits > li.banana')); // false
</script>
</html>
<!DOCTYPE html>
<head>
<style>
.red { color: red; }
.blue { color: blue; }
</style>
</head>
<html>
<body>
<ul id="fruits">
<li class="red">Apple</li>
<li class="red">Banana</li>
<li class="red">Orange</li>
</ul>
<script>
// class ๊ฐ์ด 'red'์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ HTMLCollection ๊ฐ์ฒด์ ๋ด์ ๋ฐํํ๋ค.
const $elems = document.getElementsByClassName('red');
// ์ด ์์ ์ HTMLCollection ๊ฐ์ฒด์๋ 3๊ฐ์ ์์ ๋
ธ๋๊ฐ ๋ด๊ฒจ ์๋ค.
console.log($elems); // HTMLCollection(3) [li.red, li.red, li.red]
// HTMLCollection ๊ฐ์ฒด์ ๋ชจ๋ ์์์ class ๊ฐ์ 'blue'๋ก ๋ณ๊ฒฝํ๋ค.
for (let i = 0; i < $elems.length; i++) {
$elems[i].className = 'blue';
}
// HTMLCollection ๊ฐ์ฒด์ ์์๊ฐ 3๊ฐ์์ 1๊ฐ๋ก ๋ณ๊ฒฝ๋์๋ค.
console.log($elems); // HTMLCollection(1) [li.red]
</script>
</body>
</html>
// for ๋ฌธ์ ์ญ๋ฐฉํฅ์ผ๋ก ์ํ
for (let i = $elems.length - 1; i >= 0; i--) {
$elems[i].className = 'blue';
}
// while ๋ฌธ์ผ๋ก HTMLCollection์ ์์๊ฐ ๋จ์ ์์ง ์์ ๋๊น์ง ๋ฌดํ ๋ฐ๋ณต
let i = 0;
while ($elems.length > i) {
$elems[i].className = 'blue';
}
// ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฉด์ ์ดํฐ๋ฌ๋ธ์ธ HTMLCollection์ ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ์ํ
[...$elems].forEach(elem => elem.className = 'blue');
// querySelectorAll์ DOM ์ปฌ๋ ์
๊ฐ์ฒด์ธ NodeList๋ฅผ ๋ฐํํ๋ค.
const $elems = document.querySelectorAll('.red');
// NodeList ๊ฐ์ฒด๋ NodeList.prototype.forEach ๋ฉ์๋๋ฅผ ์์๋ฐ์ ์ฌ์ฉํ ์ ์๋ค.
$elems.forEach(elem => elem.className = 'blue');
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// childNodes ํ๋กํผํฐ๋ NodeList ๊ฐ์ฒด(live)๋ฅผ ๋ฐํํ๋ค.
const { childNodes } = $fruits;
console.log(childNodes instanceof NodeList); // true
// $fruits ์์์ ์์ ๋
ธ๋๋ ๊ณต๋ฐฑ ํ
์คํธ ๋
ธ๋(39.3.1์ "๊ณต๋ฐฑ ํ
์คํธ ๋
ธ๋" ์ฐธ๊ณ )๋ฅผ ํฌํจํด ๋ชจ๋ 5๊ฐ๋ค.
console.log(childNodes); // NodeList(5) [text, li, text, li, text]
for (let i = 0; i < childNodes.length; i++) {
// removeChild ๋ฉ์๋๋ $fruits ์์์ ์์ ๋
ธ๋๋ฅผ DOM์์ ์ญ์ ํ๋ค.
// (39.6.9์ "๋
ธ๋ ์ญ์ " ์ฐธ๊ณ )
// removeChild ๋ฉ์๋๊ฐ ํธ์ถ๋ ๋๋ง๋ค NodeList ๊ฐ์ฒด์ธ childNodes๊ฐ ์ค์๊ฐ์ผ๋ก ๋ณ๊ฒฝ๋๋ค.
// ๋ฐ๋ผ์ ์ฒซ ๋ฒ์งธ, ์ธ ๋ฒ์งธ ๋ค์ฏ ๋ฒ์งธ ์์๋ง ์ญ์ ๋๋ค.
$fruits.removeChild(childNodes[i]);
}
// ์์๊ณผ ๋ค๋ฅด๊ฒ $fruits ์์์ ๋ชจ๋ ์์ ๋
ธ๋๊ฐ ์ญ์ ๋์ง ์๋๋ค.
console.log(childNodes); // NodeList(2) [li, li]
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// childNodes ํ๋กํผํฐ๋ NodeList ๊ฐ์ฒด(live)๋ฅผ ๋ฐํํ๋ค.
const { childNodes } = $fruits;
// ์คํ๋ ๋ ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํ๋ค.
[...childNodes].forEach(childNode => {
$fruits.removeChild(childNode);
});
// $fruits ์์์ ๋ชจ๋ ์์ ๋
ธ๋๊ฐ ๋ชจ๋ ์ญ์ ๋์๋ค.
console.log(childNodes); // NodeList []
</script>
</html>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
</html>
<ul id="fruits"><li
class="apple">Apple</li><li
class="banana">Banana</li><li
class="orange">Orange</li></ul>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ #fruits ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $fruits = document.getElementById('fruits');
// #fruits ์์์ ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// childNodes ํ๋กํผํฐ๊ฐ ๋ฐํํ NodeList์๋ ์์ ๋
ธ๋๋ฟ๋ง ์๋๋ผ ํ
์คํธ ๋
ธ๋๋ ํฌํจ๋์ด ์๋ค.
console.log($fruits.childNodes);
// NodeList(7) [text, li.apple, text, li.banana, text, li.orange, text]
// #fruits ์์์ ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// children ํ๋กํผํฐ๊ฐ ๋ฐํํ HTMLCollection์๋ ์์ ๋
ธ๋๋ง ํฌํจ๋์ด ์๋ค.
console.log($fruits.children);
// HTMLCollection(3) [li.apple, li.banana, li.orange]
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// firstChild ํ๋กํผํฐ๋ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
console.log($fruits.firstChild); // #text
// #fruits ์์์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// lastChild ํ๋กํผํฐ๋ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
console.log($fruits.lastChild); // #text
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// firstElementChild ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
console.log($fruits.firstElementChild); // li.apple
// #fruits ์์์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// lastElementChild ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
console.log($fruits.lastElementChild); // li.orange
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ #fruits ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $fruits = document.getElementById('fruits');
// #fruits ์์์ ์์ ๋
ธ๋๊ฐ ์กด์ฌํ๋์ง ํ์ธํ๋ค.
// hasChildNodes ๋ฉ์๋๋ ํ
์คํธ ๋
ธ๋๋ฅผ ํฌํจํ์ฌ ์์ ๋
ธ๋์ ์กด์ฌ๋ฅผ ํ์ธํ๋ค.
console.log($fruits.hasChildNodes()); // true
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ #fruits ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $fruits = document.getElementById('fruits');
// hasChildNodes ๋ฉ์๋๋ ํ
์คํธ ๋
ธ๋๋ฅผ ํฌํจํ์ฌ ์์ ๋
ธ๋์ ์กด์ฌ๋ฅผ ํ์ธํ๋ค.
console.log($fruits.hasChildNodes()); // true
// ์์ ๋
ธ๋ ์ค์ ํ
์คํธ ๋
ธ๋๊ฐ ์๋ ์์ ๋
ธ๋๊ฐ ์กด์ฌํ๋์ง๋ ํ์ธํ๋ค.
console.log(!!$fruits.children.length); // 0 -> false
// ์์ ๋
ธ๋ ์ค์ ํ
์คํธ ๋
ธ๋๊ฐ ์๋ ์์ ๋
ธ๋๊ฐ ์กด์ฌํ๋์ง๋ ํ์ธํ๋ค.
console.log(!!$fruits.childElementCount); // 0 -> false
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
<script>
// ์์ ๋
ธ๋์ ํ
์คํธ ๋
ธ๋๋ firstChild ํ๋กํผํฐ๋ก ์ ๊ทผํ ์ ์๋ค.
console.log(document.getElementById('foo').firstChild); // #text
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ .banana ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $banana = document.querySelector('.banana');
// .banana ์์ ๋
ธ๋์ ๋ถ๋ชจ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
console.log($banana.parentNode); // ul#fruits
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ #fruits ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $fruits = document.getElementById('fruits');
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// firstChild ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ฟ๋ง ์๋๋ผ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
const { firstChild } = $fruits;
console.log(firstChild); // #text
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋(ํ
์คํธ ๋
ธ๋)์ ๋ค์ ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// nextSibling ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ฟ๋ง ์๋๋ผ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
const { nextSibling } = firstChild;
console.log(nextSibling); // li.apple
// li.apple ์์์ ์ด์ ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// previousSibling ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ฟ๋ง ์๋๋ผ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
const { previousSibling } = nextSibling;
console.log(previousSibling); // #text
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// firstElementChild ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
const { firstElementChild } = $fruits;
console.log(firstElementChild); // li.apple
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ์์ ๋
ธ๋(li.apple)์ ๋ค์ ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// nextElementSibling ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
const { nextElementSibling } = firstElementChild;
console.log(nextElementSibling); // li.banana
// li.banana ์์์ ์ด์ ํ์ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// previousElementSibling ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
const { previousElementSibling } = nextElementSibling;
console.log(previousElementSibling); // li.apple
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
</body>
<script>
// ๋ฌธ์ ๋
ธ๋์ ๋
ธ๋ ์ ๋ณด๋ฅผ ์ทจ๋ํ๋ค.
console.log(document.nodeType); // 9
console.log(document.nodeName); // #document
// ์์ ๋
ธ๋์ ๋
ธ๋ ์ ๋ณด๋ฅผ ์ทจ๋ํ๋ค.
const $foo = document.getElementById('foo');
console.log($foo.nodeType); // 1
console.log($foo.nodeName); // DIV
// ํ
์คํธ ๋
ธ๋์ ๋
ธ๋ ์ ๋ณด๋ฅผ ์ทจ๋ํ๋ค.
const $textNode = $foo.firstChild;
console.log($textNode.nodeType); // 3
console.log($textNode.nodeName); // #text
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
</body>
<script>
// ๋ฌธ์ ๋
ธ๋์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ค.
console.log(document.nodeValue); // null
// ์์ ๋
ธ๋์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ค.
const $foo = document.getElementById('foo');
console.log($foo.nodeValue); // null
// ํ
์คํธ ๋
ธ๋์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ค.
const $textNode = $foo.firstChild;
console.log($textNode.nodeValue); // Hello
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
</body>
<script>
// 1. #foo ์์ ๋
ธ๋์ ์์ ๋
ธ๋์ธ ํ
์คํธ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $textNode = document.getElementById('foo').firstChild;
// 2. nodeValue ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ
์คํธ ๋
ธ๋์ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
$textNode.nodeValue = 'World';
console.log($textNode.nodeValue); // World
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// #foo ์์ ๋
ธ๋์ ํ
์คํธ๋ฅผ ๋ชจ๋ ์ทจ๋ํ๋ค. ์ด๋ HTML ๋งํฌ์
์ ๋ฌด์๋๋ค.
console.log(document.getElementById('foo').textContent); // Hello world!
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// #foo ์์ ๋
ธ๋๋ ํ
์คํธ ๋
ธ๋๊ฐ ์๋๋ค.
console.log(document.getElementById('foo').nodeValue); // null
// #foo ์์ ๋
ธ๋์ ์์ ๋
ธ๋์ธ ํ
์คํธ ๋
ธ๋์ ๊ฐ์ ์ทจ๋ํ๋ค.
console.log(document.getElementById('foo').firstChild.nodeValue); // Hello
// span ์์ ๋
ธ๋์ ์์ ๋
ธ๋์ธ ํ
์คํธ ๋
ธ๋์ ๊ฐ์ ์ทจ๋ํ๋ค.
console.log(document.getElementById('foo').lastChild.firstChild.nodeValue); // world!
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<!-- ์์ ๋
ธ๋์ ์ฝํ
์ธ ์์ญ์ ๋ค๋ฅธ ์์ ๋
ธ๋๊ฐ ์๊ณ ํ
์คํธ๋ง ์กด์ฌ -->
<div id="foo">Hello</div>
</body>
<script>
const $foo = document.getElementById('foo');
// ์์ ๋
ธ๋์ ์ฝํ
์ธ ์์ญ์ ์์ ์์ ๋
ธ๋๊ฐ ์๊ณ ํ
์คํธ๋ง ์กด์ฌํ๋ค๋ฉด
// firstChild.nodeValue์ textContent๋ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค.
console.log($foo.textContent === $foo.firstChild.nodeValue); // true
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// #foo ์์ ๋
ธ๋์ ๋ชจ๋ ์์ ๋
ธ๋๊ฐ ์ ๊ฑฐ๋๊ณ ํ ๋นํ ๋ฌธ์์ด์ด ํ
์คํธ๋ก ์ถ๊ฐ๋๋ค.
// ์ด๋ HTML ๋งํฌ์
์ด ํ์ฑ๋์ง ์๋๋ค.
document.getElementById('foo').textContent = 'Hi <span>there!</span>';
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// #foo ์์์ ์ฝํ
์ธ ์์ญ ๋ด์ HTML ๋งํฌ์
์ ๋ฌธ์์ด๋ก ์ทจ๋ํ๋ค.
console.log(document.getElementById('foo').innerHTML);
// "Hello <span>world!</span>"
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// HTML ๋งํฌ์
์ด ํ์ฑ๋์ด ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก DOM์ ๋ฐ์๋๋ค.
document.getElementById('foo').innerHTML = 'Hi <span>there!</span>';
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// ๋
ธ๋ ์ถ๊ฐ
$fruits.innerHTML += '<li class="banana">Banana</li>';
// ๋
ธ๋ ๊ต์ฒด
$fruits.innerHTML = '<li class="orange">Orange</li>';
// ๋
ธ๋ ์ญ์
$fruits.innerHTML = '';
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
</body>
<script>
// innerHTML ํ๋กํผํฐ๋ก ์คํฌ๋ฆฝํธ ํ๊ทธ๋ฅผ ์ฝ์
ํ์ฌ ์๋ฐ์คํฌ๋ฆฝํธ๊ฐ ์คํ๋๋๋ก ํ๋ค.
// HTML5๋ innerHTML ํ๋กํผํฐ๋ก ์ฝ์
๋ script ์์ ๋ด์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์คํํ์ง ์๋๋ค.
document.getElementById('foo').innerHTML
= '<script>alert(document.cookie)</script>';
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
</body>
<script>
// ์๋ฌ ์ด๋ฒคํธ๋ฅผ ๊ฐ์ ๋ก ๋ฐ์์์ผ์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๊ฐ ์คํ๋๋๋ก ํ๋ค.
document.getElementById('foo').innerHTML
= `<img src="x" onerror="alert(document.cookie)">`;
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// ๋
ธ๋ ์ถ๊ฐ
$fruits.innerHTML += '<li class="banana">Banana</li>';
</script>
</html>
$fruits.innerHTML += '<li class="banana">Banana</li>';
$fruits.innerHTML = $fruits.innerHTML + '<li class="banana">Banana</li>';
// '<li class="apple">Apple</li>' + '<li class="banana">Banana</li>'
<ul id="fruits">
<li class="apple">Apple</li>
<li class="orange">Orange</li>
</ul>
<!DOCTYPE html>
<html>
<body>
<!-- beforebegin -->
<div id="foo">
<!-- afterbegin -->
text
<!-- beforeend -->
</div>
<!-- afterend -->
</body>
<script>
const $foo = document.getElementById('foo');
$foo.insertAdjacentHTML('beforebegin', '<p>beforebegin</p>');
$foo.insertAdjacentHTML('afterbegin', '<p>afterbegin</p>');
$foo.insertAdjacentHTML('beforeend', '<p>beforeend</p>');
$foo.insertAdjacentHTML('afterend', '<p>afterend</p>');
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// 1. ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// 2. ํ
์คํธ ๋
ธ๋ ์์ฑ
const textNode = document.createTextNode('Banana');
// 3. ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(textNode);
// 4. $li ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($li);
</script>
</html>
// 1. ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// 1. ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// ์์ฑ๋ ์์ ๋
ธ๋๋ ์๋ฌด๋ฐ ์์ ๋
ธ๋๊ฐ ์๋ค.
console.log($li.childNodes); // NodeList []
// 2. ํ
์คํธ ๋
ธ๋ ์์ฑ
const textNode = document.createTextNode('Banana');
// 3. ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(textNode);
// ํ
์คํธ ๋
ธ๋๋ฅผ ์์ฑํ์ฌ ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(document.createTextNode('Banana'));
// $li ์์ ๋
ธ๋์ ์์ ๋
ธ๋๊ฐ ํ๋๋ ์๋ ์ ์ฝ๋์ ๋์ผํ๊ฒ ๋์ํ๋ค.
$li.textContent = 'Banana';
// 4. $li ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($li);
<!DOCTYPE html>
<html>
<body>
<ul id="fruits"></ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
['Apple', 'Banana', 'Orange'].forEach(text => {
// 1. ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// 2. ํ
์คํธ ๋
ธ๋ ์์ฑ
const textNode = document.createTextNode(text);
// 3. ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(textNode);
// 4. $li ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($li);
});
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits"></ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// ์ปจํ
์ด๋ ์์ ๋
ธ๋ ์์ฑ
const $container = document.createElement('div');
['Apple', 'Banana', 'Orange'].forEach(text => {
// 1. ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// 2. ํ
์คํธ ๋
ธ๋ ์์ฑ
const textNode = document.createTextNode(text);
// 3. ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(textNode);
// 4. $li ์์ ๋
ธ๋๋ฅผ ์ปจํ
์ด๋ ์์์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$container.appendChild($li);
});
// 5. ์ปจํ
์ด๋ ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($container);
</script>
</html>
<ul id="fruits">
<div>
<li>apple</li>
<li>banana</li>
<li>orange</li>
</div>
</ul>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits"></ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// DocumentFragment ๋
ธ๋ ์์ฑ
const $fragment = document.createDocumentFragment();
['Apple', 'Banana', 'Orange'].forEach(text => {
// 1. ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// 2. ํ
์คํธ ๋
ธ๋ ์์ฑ
const textNode = document.createTextNode(text);
// 3. ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(textNode);
// 4. $li ์์ ๋
ธ๋๋ฅผ DocumentFragment ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$fragment.appendChild($li);
});
// 5. DocumentFragment ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($fragment);
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
// ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(document.createTextNode('Orange'));
// $li ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
document.getElementById('fruits').appendChild($li);
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(document.createTextNode('Orange'));
// $li ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ์์ ์์ ์ฝ์
$fruits.insertBefore($li, $fruits.lastElementChild);
// Apple - Orange - Banana
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<div>test</div>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(document.createTextNode('Orange'));
// ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋
ธ๋๋ ๋ฐ๋์ #fruits ์์ ๋
ธ๋์ ์์ ๋
ธ๋์ด์ด์ผ ํ๋ค.
$fruits.insertBefore($li, document.querySelector('div'));
// DOMException
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement('li');
// ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(document.createTextNode('Orange'));
// ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋
ธ๋๊ฐ null์ด๋ฉด $li ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.insertBefore($li, null);
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// ์ด๋ฏธ ์กด์ฌํ๋ ์์ ๋
ธ๋๋ฅผ ์ทจ๋
const [$apple, $banana, ] = $fruits.children;
// ์ด๋ฏธ ์กด์ฌํ๋ $apple ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ๋
ธ๋๋ก ์ด๋
$fruits.appendChild($apple); // Banana - Orange - Apple
// ์ด๋ฏธ ์กด์ฌํ๋ $banana ์์ ๋
ธ๋๋ฅผ #fruits ์์์ ๋ง์ง๋ง ์์ ๋
ธ๋ ์์ผ๋ก ์ด๋
$fruits.insertBefore($banana, $fruits.lastElementChild);
// Orange - Banana - Apple
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
const $apple = $fruits.firstElementChild;
// $apple ์์๋ฅผ ์์ ๋ณต์ฌํ์ฌ ์ฌ๋ณธ์ ์์ฑ. ํ
์คํธ ๋
ธ๋๊ฐ ์๋ ์ฌ๋ณธ์ด ์์ฑ๋๋ค.
const $shallowClone = $apple.cloneNode();
// ์ฌ๋ณธ ์์ ๋
ธ๋์ ํ
์คํธ ์ถ๊ฐ
$shallowClone.textContent = 'Banana';
// ์ฌ๋ณธ ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($shallowClone);
// #fruits ์์๋ฅผ ๊น์ ๋ณต์ฌํ์ฌ ๋ชจ๋ ์์ ๋
ธ๋๊ฐ ํฌํจ๋ ์ฌ๋ณธ์ ์์ฑ
const $deepClone = $fruits.cloneNode(true);
// ์ฌ๋ณธ ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($deepClone);
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// ๊ธฐ์กด ๋
ธ๋์ ๊ต์ฒดํ ์์ ๋
ธ๋๋ฅผ ์์ฑ
const $newChild = document.createElement('li');
$newChild.textContent = 'Banana';
// #fruits ์์ ๋
ธ๋์ ์ฒซ ๋ฒ์งธ ์์ ์์ ๋
ธ๋๋ฅผ $newChild ์์ ๋
ธ๋๋ก ๊ต์ฒด
$fruits.replaceChild($newChild, $fruits.firstElementChild);
</script>
</html>
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์๋ฅผ DOM์์ ์ญ์
$fruits.removeChild($fruits.lastElementChild);
</script>
</html>
<input id="user" type="text" value="ungmo2">
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
// ์์ ๋
ธ๋์ attribute ํ๋กํผํฐ๋ ์์ ๋
ธ๋์ ๋ชจ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋์ ์ฐธ์กฐ๊ฐ ๋ด๊ธด NamedNodeMap ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
const { attributes } = document.getElementById('user');
console.log(attributes);
// NamedNodeMap {0: id, 1: type, 2: value, id: id, type: type, value: value, length: 3}
// ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ ์ทจ๋
console.log(attributes.id.value); // user
console.log(attributes.type.value); // text
console.log(attributes.value.value); // ungmo2
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
const $input = document.getElementById('user');
// value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋
const inputValue = $input.getAttribute('value');
console.log(inputValue); // ungmo2
// value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๋ณ๊ฒฝ
$input.setAttribute('value', 'foo');
console.log($input.getAttribute('value')); // foo
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
const $input = document.getElementById('user');
// value ์ดํธ๋ฆฌ๋ทฐํธ์ ์กด์ฌ ํ์ธ
if ($input.hasAttribute('value')) {
// value ์ดํธ๋ฆฌ๋ทฐํธ ์ญ์
$input.removeAttribute('value');
}
// value ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์ญ์ ๋์๋ค.
console.log($input.hasAttribute('value')); // false
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
const $input = document.getElementById('user');
// ์์ ๋
ธ๋์ value ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝ
$input.value = 'foo';
// ์์ ๋
ธ๋์ value ํ๋กํผํฐ ๊ฐ์ ์ฐธ์กฐ
console.log($input.value); // foo
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
const $input = document.getElementById('user');
// attributes ํ๋กํผํฐ์ ์ ์ฅ๋ value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ
console.log($input.getAttribute('value')); // ungmo2
// ์์ ๋
ธ๋์ value ํ๋กํผํฐ์ ์ ์ฅ๋ value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ
console.log($input.value); // ungmo2
</script>
</body>
</html>
// attributes ํ๋กํผํฐ์ ์ ์ฅ๋ value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋ํ๋ค. ๊ฒฐ๊ณผ๋ ์ธ์ ๋ ๋์ผํ๋ค.
document.getElementById('user').getAttribute('value'); // ungmo2
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
// HTML ์์์ ์ง์ ํ ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ, ์ฆ ์ด๊ธฐ ์ํ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
document.getElementById('user').setAttribute('value', 'foo');
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
const $input = document.getElementById('user');
// ์ฌ์ฉ์๊ฐ input ์์์ ์
๋ ฅ ํ๋์ ๊ฐ์ ์
๋ ฅํ ๋๋ง๋ค input ์์ ๋
ธ๋์
// value ํ๋กํผํฐ ๊ฐ, ์ฆ ์ต์ ์ํ ๊ฐ์ ์ทจ๋ํ๋ค. value ํ๋กํผํฐ ๊ฐ์ ์ฌ์ฉ์์ ์
๋ ฅ์
// ์ํด ๋์ ์ผ๋ก ๋ณ๊ฒฝ๋๋ค.
$input.oninput = () => {
console.log('value ํ๋กํผํฐ ๊ฐ', $input.value);
};
// getAttribute ๋ฉ์๋๋ก ์ทจ๋ํ HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ, ์ฆ ์ด๊ธฐ ์ํ ๊ฐ์ ๋ณํ์ง ์๊ณ ์ ์ง๋๋ค.
console.log('value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ', $input.getAttribute('value'));
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
const $input = document.getElementById('user');
// DOM ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ์ฌ HTML ์์์ ์ต์ ์ํ๋ฅผ ๋ณ๊ฒฝํ๋ค.
$input.value = 'foo';
console.log($input.value); // foo
// getAttribute ๋ฉ์๋๋ก ์ทจ๋ํ HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ, ์ฆ ์ด๊ธฐ ์ํ ๊ฐ์ ๋ณํ์ง ์๊ณ ์ ์ง๋๋ค.
console.log($input.getAttribute('value')); // ungmo2
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
const $input = document.getElementById('user');
// id ์ดํธ๋ฆฌ๋ทฐํธ์ id ํ๋กํผํฐ๋ ์ฌ์ฉ์ ์
๋ ฅ๊ณผ ๊ด๊ณ์์ด ํญ์ ๋์ผํ ๊ฐ์ผ๋ก ์ฐ๋ํ๋ค.
$input.id = 'foo';
console.log($input.id); // foo
console.log($input.getAttribute('id')); // foo
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<input type="checkbox" checked>
<script>
const $checkbox = document.querySelector('input[type=checkbox]');
// getAttribute ๋ฉ์๋๋ก ์ทจ๋ํ ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ธ์ ๋ ๋ฌธ์์ด์ด๋ค.
console.log($checkbox.getAttribute('checked')); // ''
// DOM ํ๋กํผํฐ๋ก ์ทจ๋ํ ์ต์ ์ํ ๊ฐ์ ๋ฌธ์์ด์ด ์๋ ์๋ ์๋ค.
console.log($checkbox.checked); // true
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul class="users">
<li id="1" data-user-id="7621" data-role="admin">Lee</li>
<li id="2" data-user-id="9524" data-role="subscriber">Kim</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul class="users">
<li id="1" data-user-id="7621" data-role="admin">Lee</li>
<li id="2" data-user-id="9524" data-role="subscriber">Kim</li>
</ul>
<script>
const users = [...document.querySelector('.users').children];
// user-id๊ฐ '7621'์ธ ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const user = users.find(user => user.dataset.userId === '7621');
// user-id๊ฐ '7621'์ธ ์์ ๋
ธ๋์์ data-role์ ๊ฐ์ ์ทจ๋ํ๋ค.
console.log(user.dataset.role); // "admin"
// user-id๊ฐ '7621'์ธ ์์ ๋
ธ๋์ data-role ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
user.dataset.role = 'subscriber';
// dataset ํ๋กํผํฐ๋ DOMStringMap ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
console.log(user.dataset); // DOMStringMap {userId: "7621", role: "subscriber"}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<ul class="users">
<li id="1" data-user-id="7621">Lee</li>
<li id="2" data-user-id="9524">Kim</li>
</ul>
<script>
const users = [...document.querySelector('.users').children];
// user-id๊ฐ '7621'์ธ ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const user = users.find(user => user.dataset.userId === '7621');
// user-id๊ฐ '7621'์ธ ์์ ๋
ธ๋์ ์๋ก์ด data ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ถ๊ฐํ๋ค.
user.dataset.role = 'admin';
console.log(user.dataset);
/*
DOMStringMap {userId: "7621", role: "admin"}
-> <li id="1" data-user-id="7621" data-role="admin">Lee</li>
*/
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<div style="color: red">Hello World</div>
<script>
const $div = document.querySelector('div');
// ์ธ๋ผ์ธ ์คํ์ผ ์ทจ๋
console.log($div.style); // CSSStyleDeclaration { 0: "color", ... }
// ์ธ๋ผ์ธ ์คํ์ผ ๋ณ๊ฒฝ
$div.style.color = 'blue';
// ์ธ๋ผ์ธ ์คํ์ผ ์ถ๊ฐ
$div.style.width = '100px';
$div.style.height = '100px';
$div.style.backgroundColor = 'yellow';
</script>
</body>
</html>
$div.style.backgroundColor = 'yellow';
$div.style['background-color'] = 'yellow';
$div.style.width = '100px';
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px; height: 100px;
background-color: antiquewhite;
}
.red { color: red; }
.blue { color: blue; }
</style>
</head>
<body>
<div class="box red">Hello World</div>
<script>
const $box = document.querySelector('.box');
// .box ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋
console.log($box.className); // 'box red'
// .box ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ ์ค์์ 'red'๋ง 'blue'๋ก ๋ณ๊ฒฝ
$box.className = $box.className.replace('red', 'blue');
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px; height: 100px;
background-color: antiquewhite;
}
.red { color: red; }
.blue { color: blue; }
</style>
</head>
<body>
<div class="box red">Hello World</div>
<script>
const $box = document.querySelector('.box');
// .box ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ์ ๋ณด๋ฅผ ๋ด์ DOMTokenList ๊ฐ์ฒด๋ฅผ ์ทจ๋
// classList๊ฐ ๋ฐํํ๋ DOMTokenList ๊ฐ์ฒด๋ HTMLCollection๊ณผ NodeList์ ๊ฐ์ด
// ๋
ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณํ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ๋ ์ด์ ์๋(live) ๊ฐ์ฒด๋ค.
console.log($box.classList);
// DOMTokenList(2) [length: 2, value: "box blue", 0: "box", 1: "blue"]
// .box ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ ์ค์์ 'red'๋ง 'blue'๋ก ๋ณ๊ฒฝ
$box.classList.replace('red', 'blue');
</script>
</body>
</html>
$box.classList.add('foo'); // -> class="box red foo"
$box.classList.add('bar', 'baz'); // -> class="box red foo bar baz"
$box.classList.remove('foo'); // -> class="box red bar baz"
$box.classList.remove('bar', 'baz'); // -> class="box red"
$box.classList.remove('x'); // -> class="box red"
$box.classList.item(0); // -> "box"
$box.classList.item(1); // -> "red"
$box.classList.contains('box'); // -> true
$box.classList.contains('blue'); // -> false
$box.classList.replace('red', 'blue'); // -> class="box blue"
$box.classList.toggle('foo'); // -> class="box blue foo"
$box.classList.toggle('foo'); // -> class="box blue"
// class ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ๋ก 'foo' ํด๋์ค๋ฅผ ์ถ๊ฐ
$box.classList.toggle('foo', true); // -> class="box blue foo"
// class ์ดํธ๋ฆฌ๋ทฐํธ์์ ๊ฐ์ ๋ก 'foo' ํด๋์ค๋ฅผ ์ ๊ฑฐ
$box.classList.toggle('foo', false); // -> class="box blue"
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: red;
}
.box {
width: 100px;
height: 50px;
background-color: cornsilk;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="box">Box</div>
<script>
const $box = document.querySelector('.box');
// .box ์์์ ์ ์ฉ๋ ๋ชจ๋ CSS ์คํ์ผ์ ๋ด๊ณ ์๋ CSSStyleDeclaration ๊ฐ์ฒด๋ฅผ ์ทจ๋
const computedStyle = window.getComputedStyle($box);
console.log(computedStyle); // CSSStyleDeclaration
// ์๋ฒ ๋ฉ ์คํ์ผ
console.log(computedStyle.width); // 100px
console.log(computedStyle.height); // 50px
console.log(computedStyle.backgroundColor); // rgb(255, 248, 220)
console.log(computedStyle.border); // 1px solid rgb(0, 0, 0)
// ์์ ์คํ์ผ(body -> .box)
console.log(computedStyle.color); // rgb(255, 0, 0)
// ๊ธฐ๋ณธ ์คํ์ผ
console.log(computedStyle.display); // block
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.box:before {
content: 'Hello';
}
</style>
</head>
<body>
<div class="box">Box</div>
<script>
const $box = document.querySelector('.box');
// ์์ฌ ์์ :before์ ์คํ์ผ์ ์ทจ๋ํ๋ค.
const computedStyle = window.getComputedStyle($box, ':before');
console.log(computedStyle.content); // "Hello"
</script>
</body>
</html>