DOMμ HTML λ¬Έμμ κ³μΈ΅μ ꡬ쑰μ μ 보λ₯Ό νννλ©° μ΄λ₯Ό μ μ΄ν μ μλ API, μ¦ νλ‘νΌν°μ λ©μλλ₯Ό μ 곡νλ μλ£κ΅¬μ‘°λ€.
HTML μμλ λ λλ§ μμ§μ μν΄ νμ±λμ΄ DOMμ ꡬμ±νλ μμ λ Έλ κ°μ²΄λ‘ λ³νλλ€. μ΄λ HTML μμμ μ΄νΈλ¦¬λ·°νΈλ μ΄νΈλ¦¬λ·°νΈ λ Έλλ‘, HTML μμμ ν μ€νΈ μ½ν μΈ λ ν μ€νΈ λ Έλλ‘ λ³νλλ€. HTML μμλ μ€μ²© κ΄κ³λ₯Ό κ°λλ° μ΄λ‘ μΈν΄ μμκ° κ³μΈ΅μ μΈ λΆμ κ΄κ³κ° νμ±λλ€. μ΄λ¬ν HTMl μμκ°μ λΆμ κ΄κ³λ₯Ό λ°μνμ¬ HTMl λ¬Έμμ κ΅¬μ± μμμΈ HTMl μμλ₯Ό κ°μ²΄νν λͺ¨λ λ Έλ κ°μ²΄λ€μ νΈλ¦¬ μλ£ κ΅¬μ‘°λ‘ κ΅¬μ±νλ€.
DOMμ λ Έλ κ°μ²΄λ μ’ λ₯κ° μκ³ μμ ꡬ쑰λ₯Ό κ°λλ€. λ Έλ κ°μ²΄λ μ΄ 12κ°μ μ’ λ₯(λ Έλ νμ )κ° μλ€.
DOMμ ꡬμ±νλ λ Έλ κ°μ²΄λ μμ μ ꡬ쑰μ μ 보λ₯Ό μ μ΄ν μ μλ DOM APIλ₯Ό μ¬μ©ν μ μλ€. DOMμ ꡬμ±νλ λ Έλ κ°μ²΄λ ECMAScript μ¬μμ μ μλ νμ€ λΉνΈμΈ κ°μ²΄κ° μλλΌ λΈλΌμ°μ νκ²½μμ μΆκ°μ μΌλ‘ μ 곡νλ νΈμ€νΈ κ°μ²΄λ€. νμ§λ§ λ Έλ κ°μ²΄λ μλ°μ€ν¬λ¦½νΈ κ°μ²΄μ΄λ―λ‘ νλ‘ν νμ μ μν μμ ꡬ쑰λ₯Ό κ°λλ€. λ Έλ κ°μ²΄μ μμꡬ쑰λ λ€μκ³Ό κ°λ€.
λ°°μ΄μ΄ κ°μ²΄μΈ λμμ λ°°μ΄μΈ κ²μ²λΌ input μμ λ Έλ κ°μ²΄λ λ€μκ³Ό κ°μ΄ λ€μν νΉμ±μ κ°λ κ°μ²΄μ΄λ©°, μ΄λ¬ν νΉμ±μ λνλ΄λ κΈ°λ₯λ€μ μμμ ν΅ν΄ μ 곡λ°λλ€.
λ Έλ κ°μ²΄μ μμ ꡬ쑰λ κ°λ°μ λꡬμ Elements ν¨λ μ°μΈ‘μ Properties ν¨λμμ νμΈν μ μλ€.
μ 리νμλ©΄, DOMμ HTML λ¬Έμμ κ³μΈ΅μ ꡬ쑰μ μ 보λ₯Ό νννλ κ²μ λ¬Όλ‘ λ Έλ κ°μ²΄μ μ’ λ₯, μ¦ λ νμ μ λ°λΌ νμν κΈ°λ₯μ νλ‘νΌν°μ λ©μλμ μ§ν©μΈ DOM APIλ‘ μ 곡νλ€. μ΄ DOM APIλ₯Ό ν΅ν΄ HTMLμ ꡬ쑰λ λ΄μ© λλ μ€νμΌ λ±μ λμ μΌλ‘ μ‘°μν μ μλ€.
HTMLμ ꡬ쑰λ λ΄μ© λλ μ€νμΌ λ±μ λμ μΌλ‘ μ‘°μνλ €λ©΄ λ¨Όμ μμ λ Έλλ₯Ό μ·¨λν΄μΌ νλ€. ν μ€νΈ λ Έλλ μμ λ Έλμ μμ λ Έλμ΄κ³ , μ΄νΈλ¦¬λ·°νΈ λ Έλλ μμ λ Έλμ μ°κ²°λμ΄ μκΈ° λλ¬Έμ΄λ€. λ°λΌμ DOMμ μμ λ Έλλ₯Ό μ·¨λν μ μλ λ€μν λ©μλλ₯Ό μ 곡νλ€.
getElementById
Document.prototype.getElementById
λ©μλλ μΈμλ‘ μ λ¬ν id μ΄νΈλ¦¬λ·°νΈ κ°μ κ°λ νλμ μμ λ
Έλλ₯Ό νμνμ¬ λ°ννλ€. getElementById
λ©μλλ Document.prototypeμ νλ‘νΌν°λ€. λ°λΌμ λ°λμ λ¬Έμ λ
ΈλμΈ documentλ₯Ό ν΅ν΄ νΈμΆν΄μΌνλ€.
<!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>
id κ°μ HTML λ¬Έμ λ΄μμ μ μΌν κ°μ΄μ΄μΌ νλ©°, class μ΄νΈλ¦¬λ·°νΈμλ λ¬λ¦¬ 곡백 λ¬Έμλ‘ κ΅¬λΆνμ¬ μ¬λ¬ κ°μ κ°μ κ°μ§ μ μλ€. λ¨, HTML λ¬Έμ λ΄μ μ€λ³΅λ idκ°μ κ°λ HTML μμκ° μ¬λ¬ κ° μ‘΄μ¬νλλΌλ μ΄λ ν μλ¬λ λ°μνμ§ μλλ€.
HTML μμμ id μ΄νΈλ¦¬λ·°νΈλ₯Ό λΆμ¬νλ©΄ id κ°κ³Ό λμΌν μ΄λ¦μ μ μ λ³μκ° μ묡μ μΌλ‘ μ μΈλκ³ ν΄λΉ λ Έλ κ°μ²΄κ° ν λΉλλ λΆμ ν¨κ³Όκ° μλ€.
<!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>
getElementsByTagName
Document.prototype
/Element.prototype.getElementByTagName
λ©μλλ μΈμλ‘ μ λ¬ν νκ·Έ μ΄λ¦μ κ°λ λͺ¨λ μμ λ
Έλλ€μ νμνμ¬ HTMLCollection κ°μ²΄λ‘ λ°ννλ€. HTMLCollection κ°μ²΄λ μ μ¬ λ°°μ΄ κ°μ²΄μ΄λ©΄μ μ΄ν°λ¬λΈμ΄λ€.
<!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>
getElementsByTagName
λ©μλλ Document.prototypeμ μ μλ λ©μλμ Element.prototypeμ μ μλ λ©μλκ° μλλ° Document.prototype.getElementsByTagName
λ©μλλ documentλ₯Ό ν΅ν΄ νΈμΆνλ©° DOM μ 체μμ μμ λ
Έλλ₯Ό νμνμ¬ λ°ννλ€.
νμ§λ§ Element.prototype.getElementsByTagName
@ λ©μλλ νΉμ μμ λ
Έλλ₯Ό ν΅ν΄ νΈμΆνλ©°, νΉμ μμ λ
Έλμ μμ λ
Έλ μ€μμ μμ λ
Έλλ₯Ό νμνμ¬ λ°ννλ€.
getElementsByClassName
Document.prototype
/Element.prototype.getElementsByClassName
λ©μλλ μΈμλ‘ μ λ¬ν class μ΄νΈλ¦¬λ·°νΈ κ°μ κ°λ λͺ¨λ μμ λ
Έλλ€μ νμνμ¬ HTMLCollection κ°μ²΄λ‘ λ°ννλ€
<!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>
querySelector
/querySelectorAll
CSS μ νμλ μ€νμΌμ μ μ©νκ³ μ νλ 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 { ... }
Document.prototype
/Select.prototype.querySelector
λ©μλλ μΈμλ‘ μ λ¬ν CSS μ νμλ₯Ό λ§μ‘±μν€λ νλμ μμ λ
Έλλ₯Ό νμνμ¬ λ°ννλ€.
<!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>
Document.prototype
/Select.prototype.querySelector
λ©μλλ μΈμλ‘ μ λ¬ν CSS μ νμλ₯Ό λ§μ‘±μν€λ λͺ¨λ μμ λ
Έλλ₯Ό νμνμ¬ NodeList κ°μ²΄λ‘ λ°ννλ€. NodeList κ°μ²΄λ μ μ¬ λ°°μ΄ κ°μ²΄μ΄λ©΄μ μ΄ν°λ¬λΈμ΄λ€.
<!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>
Element.prototype.matches
λ©μλλ μΈμλ‘ μ λ¬ν CSS μ νμλ₯Ό ν΅ν΄ νΉμ μμ λ
Έλλ₯Ό μ·¨λν μ μλμ§ νμΈνλ€.
<!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>
HTMLCollectionκ³Ό NodeListλ DOM APIκ° μ¬λ¬ κ°μ κ²°κ³Ό κ°μ λ°ννκΈ° μν DOM 컬λ μ κ°μ²΄λ‘, λͺ¨λ μ μ¬ λ°°μ΄ κ°μ²΄μ΄λ©΄μ μ΄ν°λ¬λΈμ΄λ€.
HTMLCollectionκ³Ό NodeListμ μ€μν νΉμ§μ λ Έλ κ°μ²΄μ μν λ³νλ₯Ό μ€μκ°μΌλ‘ λ°μνλ μ΄μ μλ(live) κ°μ²΄λΌλ κ²μ΄λ€.
getElementsByTagName
, getElementsByClassName
λ©μλκ° λ°ννλ HTMLCollection κ°μ²΄λ λ
Έλ κ°μ²΄μ μν λ³νλ₯Ό μ€μκ°μΌλ‘ λ°μνλ μ΄μ μλ DOM 컬λ μ
κ°μ²΄λ€. λ°λΌμ HTMLCollection κ°μ²΄λ₯Ό forλ¬ΈμΌλ‘ μννλ©΄μ λ
Έλ κ°μ²΄μ μνλ₯Ό λ³κ²½ν΄μΌ ν λ μ£Όμν΄μΌ νλ€.getElementsByTagName
, getElementsByClassName
λμ querySelectorAll
λ©μλλ₯Ό μ¬μ©νλ λ°©λ²λ μλ€. querySelectorAll
λ©μλλ DOM 컬λ μ
κ°μ²΄μΈ NodeList κ°μ²΄λ₯Ό λ°ννλ€. μ΄λ NodeList κ°μ²΄λ μ€μκ°μΌλ‘ λ
Έλμ μν λ³κ²½μ λ°μνμ§ μλ(non-live) κ°μ²΄λ€. νμ§λ§ childNodes νλ‘νΌν°κ° λ°ννλ NodeList κ°μ²΄λ HTMLCollection κ°μ²΄μ κ°μ΄ μ€μκ°μΌλ‘ λ
Έλ κ°μ²΄μ μν λ³κ²½μ λ°μνλ libe κ°μ²΄λ‘ λμνλ―λ‘ μ£Όμκ° νμνλ€.λ°λΌμ λ Έλ κ°μ²΄μ μν λ³κ²½κ³Ό μκ΄μμ΄ μμ νκ² DOM 컬λ μ μ μ¬μ©νλ €λ©΄ HTMLCollection μ΄λ NodeList κ°μ²΄λ₯Ό λ°°μ΄λ‘ λ³ννμ¬ μ¬μ©νλ κ²μ κΆμ₯νλ€.
HTMLCollectionκ³Ό NodeList κ°μ²΄λ λͺ¨λ μ μ¬ λ°°μ΄ κ°μ²΄μ΄λ©΄μ μ΄ν°λ¬λΈμ΄λ―λ‘ μ€νλ λ λ¬Έλ²μ΄λ Array.from
λ©μλλ₯Ό μ¬μ©ν΄ κ°λ¨ν λ°°μ΄λ‘ λ³νν μ μλ€.
Node, Element μΈν°νμ΄μ€λ DOM νΈλ¦¬μμ λ Έλλ₯Ό νμν μ μλλ‘ νΈλ¦¬ νμ νλ‘νΌν°λ₯Ό μ 곡νλ€.
parentNode
, previousSibling
, firstChild
, childNodes
νλ‘νΌν°λ Node.prototypeμ΄ μ 곡νκ³ , νλ‘νΌν°ν€μ Elementκ° ν¬ν¨λ previousElementSibling
, nextElementSibling
κ³Ό children
νλ‘νΌν°λ Element.prototypeμ΄ μ 곡νλ€.
λ Έλ νμ νλ‘νΌν°λ setter μμ΄ getterλ§ μ‘΄μ¬νλ μ½κΈ° μ μ© μ κ·Όμ νλ‘νΌν°μ΄λ€.
HTML μμ μ¬μ΄μ μ€νμ΄μ€, ν, μ€λ°κΏ(κ°ν) λ±μ 곡백 λ¬Έμλ ν μ€νΈ λ Έλλ₯Ό μμ±νλ€.
Node.prototype.childNodes
μμ λ
Έλλ₯Ό λͺ¨λ νμνμ¬ DOM 컬λ μ
κ°μ²΄μΈ NodeListμ λ΄μ λ°ννλ€.Β childNodes νλ‘νΌν°κ° λ°νν NodeListμλ μμ λ
ΈλλΏλ§ μλλΌ ν
μ€νΈ λ
Έλλ ν¬ν¨λμ΄ μμ μ μλ€.Element.prototype.children
μμ λ
Έλ μ€μμ μμ λ
Έλλ§ λͺ¨λ νμνμ¬ DOM 컬λ μ
κ°μ²΄μΈ HTMLCollectionμ λ΄μ λ°ννλ€.Β children νλ‘νΌν°κ° λ°νν HTMLCollectionμλ ν
μ€νΈ λ
Έλκ° ν¬ν¨λμ§ μλλ€.Node.prototype.firstChild
첫 λ²μ§Έ μμ λ
Έλλ₯Ό λ°ννλ€. ν
μ€νΈ λ
Έλμ΄κ±°λ μμ λ
Έλλ₯Ό λ°ννλ€.Node.prototype.lastChild
λ§μ§λ§ μμ λ
Έλλ₯Ό λ°ννλ€. ν
μ€νΈ λ
Έλμ΄κ±°λ μμ λ
Έλλ₯Ό λ°ννλ€.Element.prototype.firstElementChild
첫 λ²μ§Έ μμ μμ λ
Έλλ₯Ό λ°ννλ€. μμ λ
Έλλ₯Ό λ°ννλ€.Element.prototype.lastElementChild
λ§μ§λ§ μμ μμ λ
Έλλ₯Ό λ°ννλ€. μμ λ
Έλλ₯Ό λ°ννλ€.<!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>
Node.prototype.hasChildNodes
λ©μλλ μμ λ
Έλμ μ‘΄μ¬λ₯Ό λΆμΈλ‘ λ°ννλ€. μ΄λ, μμ λ
Έλμμ ν
μ€νΈ λ
Έλλ₯Ό ν¬ν¨νλ€.
<!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>
μμ λ
Έλμ€ ν
μ€νΈ λ
Έλκ° μλ μμ λ
Έλκ° μ‘΄μ¬νλμ§ νμΈνλ €λ©΄children.length
λλ Element μΈν°νμ΄μ€μ childElementCount νλ‘νΌν°λ₯Ό μ¬μ©νλ€.
<!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>
firstChild λ©μλλ₯Ό μ¬μ©ν΄ μμ λ Έλμ ν μ€νΈ λ Έλμ μ κ·Όν μ μλ€.
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
<script>
// μμ λ
Έλμ ν
μ€νΈ λ
Έλλ firstChild νλ‘νΌν°λ‘ μ κ·Όν μ μλ€.
console.log(document.getElementById('foo').firstChild); // #text
</script>
</body>
</html>
Node.prototype.parentNode νλ‘νΌν°λ₯Ό μ¬μ©ν΄λ¦¬ν λ ΈλμΈ λΆλͺ¨λ Έλλ₯Ό νμν μ μλ€.
<!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>
λΆλͺ¨ λ Έλκ° κ°μ νμ λ Έλλ₯Ό νμνλ €λ©΄ λ€μκ³Ό κ°μ λ Έλ νμ νλ‘νΌν°λ₯Ό μ¬μ©νλ€.
Node.prototype.previousSibling
λΆλͺ¨ λ
Έλκ° κ°μ νμ λ
Έλ μ€μμ μμ μ μ΄μ νμ λ
Έλλ₯Ό νμνμ¬ λ°ννλ€. previousSibling νλ‘νΌν°κ° λ°ννλ νμ λ
Έλλ μμ λ
ΈλλΏλ§ μλλΌ ν
μ€νΈ λ
ΈλμΌ μλ μλ€.Node.prototype.nextSibling
λΆλͺ¨ λ
Έλκ° κ°μ νμ λ
Έλ μ€μμ μμ μ λ€μ νμ λ
Έλλ₯Ό νμνμ¬ λ°ννλ€. μμ λ
ΈλλΏλ§ μλλΌ ν
μ€νΈ λ
ΈλμΌ μλ μλ€.Element.prototype.previousElementSibling
λΆλͺ¨ λ
Έλκ° κ°μ νμ λ
Έλ μ€μμ μμ μ μ΄μ νμ μμ λ
Έλλ₯Ό νμνμ¬ λ°ννλ€. μμ λ
Έλλ§ λ°ννλ€.Element.prototype.nextElementSibling
λΆλͺ¨ λ
Έλκ° κ°μ νμ λ
Έλ μ€μμ μμ μ λ€μ νμ μμ λ
Έλλ₯Ό νμνμ¬ λ°ννλ€. μμ λ
Έλλ§ λ°ννλ€.<!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>
Node.prototype.nodeType
: λ
Έλ κ°μ²΄μ μ’
λ₯, μ¦ λ
Έλ νμ
μ λνλ΄λ μμλ₯Ό λ°ννλ€. λ
Έλ νμ
μμλ Nodeμ μ μλμ΄ μλ€. Node.ELEMENT_NODE: μμ λ
Έλ νμ
μ λνλ΄λ μμ 1μ λ°ν Node.TEXT_NODE: ν
μ€νΈ λ
Έλ νμ
μ λνλ΄λ μμ 3μ λ°ν Node.DOCUMENT_NODE: λ¬Έμ λ
Έλ νμ
μ λνλ΄λ μμ 9λ₯Ό λ°νNode.prototype.nodeName
: λ
Έλμ μ΄λ¦μ λ¬Έμμ΄λ‘ λ°ννλ€. μμ λ
Έλ: λλ¬Έμ λ¬Έμμ΄λ‘ νκ·Έ μ΄λ¦("UL", "LI"λ±)μ λ°ν ν
μ€νΈ λ
Έλ: λ¬Έμμ΄ "#text"λ₯Ό λ°ν λ¬Έμ λ
Έλ: λ¬Έμμ΄ "#document"λ₯Ό λ°ν<!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>
λ
Έλ νμ, λ
Έλ μ 보 νλ‘νΌν°λ λͺ¨λ μ½κΈ° μ μ© μ κ·Όμ νλ‘νΌν°μ΄μ§λ§, Node.prototype.nodeValue
νλ‘νΌν°λ setterμ getter λͺ¨λ μ‘΄μ¬νλ μ κ·Όμ νλ‘νΌν°λ‘ μ°Έμ‘°μ ν λΉμ΄ λͺ¨λ κ°λ₯νλ€.
λ Έλ κ°μ²΄μ nodeValue νλ‘νΌν°λ₯Ό μ°Έμ‘°νλ©΄ λ Έλ κ°μ²΄μ κ°μ λ°ννλ€. μ΄ λ λ Έλ κ°μ²΄μ κ°μ ν μ€νΈ λ Έλμ ν μ€νΈμ΄λ©° λ¬Έμ λ Έλλ μμ λ ΈλμΌ κ²½μ° nullμ λ°ννλ€.
<!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>
ν μ€νΈ λ Έλμ nodeValue νλ‘νΌν°μ κ°μ ν λΉνλ©΄ ν μ€νΈ λ Έλμ κ°μ λ³κ²½ν μ μλ€. λ°λΌμ μμ λ Έλμ ν μ€νΈλ₯Ό λ³κ²½νλ €λ©΄ λ€μκ³Ό κ°μ μμμ μ²λ¦¬κ° νμνλ€.
<!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>
**Node.prototype.textContent
νλ‘νΌν°λ** setterμ getter λͺ¨λ μ‘΄μ¬νλ μ κ·Όμ νλ‘νΌν°λ‘ μμ λ
Έλμ ν
μ€νΈμ λͺ¨λ μμ λ
Έλμ ν
μ€νΈλ₯Ό λͺ¨λ μ·¨λνκ±°λ λ³κ²½νλ€.
μμ λ Έλμ textContent νλ‘νΌν°λ₯Ό μ°Έμ‘°νλ©΄ μμ λ Έλμ μ½ν μΈ μμ(μμ νκ·Έμ μ’ λ£ νκ·Έ μ¬μ΄) λ΄μ ν μ€νΈλ₯Ό λͺ¨λ λ°ννλ€. λ€μ λ§ν΄, μμ λ Έλμ childNodes νλ‘νΌν°κ° λ°νν λͺ¨λ λ Έλλ€μ ν μ€νΈ λ Έλμ κ°, μ¦ ν μ€νΈλ₯Ό λͺ¨λ λ°ννλ€.
<!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>
μ°Έκ³ λ‘ textContent νλ‘νΌν°μ μ μ¬ν λμμ νλ innerTextλ λ€μκ³Ό κ°μ μ΄μ λ‘ μ¬μ©νμ§ μλκ²μ΄ μ’λ€.
DOM μ‘°μμ μλ‘μ΄ λ Έλλ₯Ό μμ±νμ¬ DOMμ μΆκ°νκ±°λ κΈ°μ‘΄ λ Έλλ₯Ό μμ λλ κ΅μ²΄νλ κ²μ λ§νλ€. DOM μ‘°μμ μν΄ DOMμ μλ‘μ΄ λ Έλκ° μΆκ°λκ±°λ μμ λλ©΄ 리νλ‘μ°μ 리νμΈνΈκ° λ°μνκΈ° λλ¬Έμ μ±λ₯μ μν΄ μ£Όμν΄μΌ νλ€.
**Element.prototype.innerHTML
νλ‘νΌν°λ setterμ getter λͺ¨λ μ‘΄μ¬νλ μ κ·Όμ νλ‘νΌν°λ‘ μμ λ
Έλμ HTML λ§ν¬μ
μ μ·¨λνκ±°λ λ³κ²½**νλ€.
μμ λ Έλμ innerHTML νλ‘νΌν°λ₯Ό μ°Έμ‘°νλ©΄ μμ λ Έλμ μ½ν μΈ μμ(μμ νκ·Έμ μ’ λ£ νκ·Έ μ¬μ΄) μ ν¬ν¨λ λͺ¨λ HTML λ§ν¬μ μ λ¬Έμμ΄λ‘ λ°ννλ€.
λ΄μ ν μ€νΈλ₯Ό λͺ¨λ λ°ννλ€. λ€μ λ§ν΄, μμ λ Έλμ childNodes νλ‘νΌν°κ° λ°νν λͺ¨λ λ Έλλ€μ ν μ€νΈ λ Έλμ κ°, μ¦ ν μ€νΈλ₯Ό λͺ¨λ λ°ννλ€
<!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>
μμ μ΄ν΄λ³Έ textContent νλ‘νΌν°λ₯Ό μ°Έμ‘°νλ©΄ HTML λ§ν¬μ μ 무μνκ³ ν μ€νΈλ§ λ°ννμ§λ§, innerHTMLμ μ¬μ©νλ©΄ νλ‘νΌν°λ HTML λ§ν¬μ μ΄ ν¬ν¨λ λ¬Έμμ΄μ κ·Έλλ‘ λ°ννλ€. μ΄μ²λΌ innerHTMLμ μ¬μ©ν DOM μ‘°μμ ꡬνμ΄ κ°νΈνκ³ μ§κ΄μ μ΄μ§λ§ ν¬λ‘μ€ μ¬μ΄νΈ μ€ν¬λ¦½ν 곡격μ μ·¨μ½νλ©°, μ½μ μμΉ λν μ§μ ν μ μμ΄ λΉν¨μ¨μ μ΄λ€.
**Element.prototype.inserdAdjacentHTML
(position, DOMString) λ©μλλ κΈ°μ‘΄ μμλ₯Ό μ κ±°νμ§ μμΌλ©΄μ μμΉλ₯Ό μ§μ ν΄ μλ‘μ΄ μμλ₯Ό μ½μ
**νλ€.
insertAdjacentHTML
λ©μλλ λ λ²μ§Έ μΈμλ‘ μ λ¬ν HTML λ§ν¬μ
λ¬Έμμ΄(DOMString)μ νμ±νκ³ κ·Έ κ²°κ³Όλ‘ μμ±λ λ
Έλλ₯Ό 첫 λ²μ§Έ μΈμλ‘ μ λ¬ν μμΉ(position)μ μ½μ
νμ¬ DOMμ λ°μνλ€. 첫 λ²μ§Έ μΈμλ‘ μ λ¬ν μ μλ λ¬Έμμ΄μ βbeforebeginβ, βafterbeginβ, βbeforeendβ, βafterbeginβ μ 4κ°μ§λ€.
<!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>
innerHTML νλ‘νΌν°λ³΄λ€ ν¨μ¨μ μ΄κ³ λΉ λ₯΄λ, 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>
Document.prototype.createElement(tagName)
λ©μλλ μμ λ
Έλλ₯Ό μμ±νμ¬ λ°ννλ€. createElement λ©μλμ 맀κ°λ³μ tagNameμλ νκ·Έ μ΄λ¦μ λνλ΄λ λ¬Έμμ΄μ μΈμλ‘ μ λ¬ νλ€. μ΄λ createElement λ©μλλ‘ μμ±ν μμ λ
Έλλ DOMμ μΆκ°λμ§ μκ³ νλ‘ μ‘΄μ¬νλ μνμ΄λ―λ‘ λ³λλ‘ DOMμ μΆκ°ν΄ μ€μΌ νλ€.Document.prototype.createTextNode(text)
λ©μλλ ν
μ€νΈ λ
Έλλ₯Ό μμ±νμ¬ λ°ννλ€. createTextNode λ©μλμ 맀κ°λ³μ textμλ ν
μ€νΈ λ
Έλμ κ°μΌλ‘ μ¬μ©ν λ¬Έμμ΄μ μΈμλ‘ μ λ¬ νλ€. createTextNode λ©μλκ° μμ±ν ν
μ€νΈ λ
Έλ λν μμλ
Έλμ μΆκ°νλ λ³λμ μ²λ¦¬κ° νμνλ€.**Node.prototype.appendChild(childNode)
λ©μλλ 맀κ°λ³μ childNodeμκ² μΈμλ‘ μ λ¬ν λ
Έλλ₯Ό appendChild λ©μλλ₯Ό νΈμΆν λ
Έλμ λ§μ§λ§ μμ λ
Έλλ‘ μΆκ°**νλ€. appendChild λ©μλμ μΈμλ‘ createTextNode λ©μλλ‘ μμ±ν ν
μ€νΈ λ
Έλλ₯Ό μ λ¬νλ©΄ appendChild λ©μλλ₯Ό νΈμΆν λ
Έλμ λ§μ§λ§μμ λ
Έλλ‘ ν
μ€νΈ λ
Έλκ° μΆκ°λλ€. μ΄ κ³Όμ μ ν΅ν΄ Document.prototype.createElement(tagName)
λ‘ μμ±ν μμλ
Έλμ Document.prototype.createTextNode(text)
λ‘ μμ±ν ν
μ€νΈ λ
Έλλ λΆμ κ΄κ³λ‘ μ°κ²°λμμ§λ§ DOMμ μΆκ°λμ§λ μμ μνμ΄λ€.<!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>
μμ κ°μ΄ μ¬λ¬ κ°μ μμ λ Έλλ₯Ό μμ±νμ¬ DOMμ μΆκ°ν μ μμΌλ μμ μμ λ 리νλ‘μ°/리νμΈν λν 3λ² λ³κ²½λκΈ° λλ¬Έμ λμ λΉμ©μ΄ λ€κ³ μ±λ₯μ μ’μ§μλ€.
λ°λΌμ DocumentFragment λ Έλλ₯Ό ν΅ν΄ ν΄κ²°ν μ μλ€. DocumentFragment λ Έλλ λ¬Έμ, μμ, μ΄νΈλ¦¬λ·°νΈ, ν μ€νΈ λ Έλμ κ°μ λ Έλ κ°μ²΄μ μΌμ’ μΌλ‘, λΆλͺ¨ λ Έλκ° μμ΄μ κΈ°μ‘΄ DOMκ³Όλ λ³λλ‘ μ‘΄μ¬νλ€λ νΉμ§μ΄ μλ€. DocumentFragment λ Έλλ μμ λ Έλλ€μ λΆλͺ¨ λ Έλλ‘μ λ³λμ μλΈ DOMμ ꡬμ±νμ¬ κΈ°μ‘΄ DOM μ μΆκ°νκΈ° μν μ©λλ‘ μ¬μ©νλ€.
<!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>
DocumentFragment λ Έλλ κΈ°μ‘΄ DOMκ³Όλ λ³λλ‘ μ‘΄μ¬νλ―λ‘ DocumentFragment λ Έλμ μμ λ Έλλ₯Ό μΆκ° νμ¬λ κΈ°μ‘΄ DOMμλ μ΄λ ν λ³κ²½λ λ°μνμ§ μλλ€. λν DocumentFragment λ Έλλ₯Ό DOMμ μΆκ°νλ©΄ μμ μ μ κ±°λκ³ μμ μ μμ λ Έλλ§ DOMμ μΆκ°λλ€.
μ΄ λ μ€μ λ‘ DOM λ³κ²½μ΄ λ°μνλ κ²μ ν λ² λΏμ΄λ©° 리νλ‘μ°μ 리νμΈνΈλ ν λ²λ§ μ€νλλ€. λ°λΌμ μ¬λ¬ κ°μ μμ λ Έλλ₯Ό DOMμ μΆκ°νλ κ²½μ° DocumentFragment λ Έλλ₯Ό μ¬μ©νλκ²μ΄ λ ν¨μ¨μ μ΄λ€.
**Node.prototype.appendChild
λ©μλλ μΈμλ‘ μ λ¬λ°μ λ
Έλλ₯Ό μμ μ νΈμΆν λͺ¨λμ λ§μ§λ§ μμ λ
Έλλ‘ DOMμ μΆκ°**νλ€. μ΄λ λ
Έλλ₯Ό μΆκ°ν μμΉλ₯Ό μ§μ ν μ μκ³ μΈμ λ λ§μ§λ§ μμ λ
Έλλ‘ μΆκ°νλ€.<!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>
Node.prototype.insertBefore(newNode, childNode)
λ©μλλ 첫 λ²μ§Έ μΈμλ‘ μ λ¬λ°μ λ
Έλλ₯Ό λ λ²μ§Έ μΈμλ‘ μ λ¬λ°μ λ
Έλ μμ μ½μ
νλ€.<!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>
λ λ²μ§Έ μΈμλ‘ μ λ¬λ°μ λ
Έλλ λ°λμ insertBefore λ©μλλ₯Ό νΈμΆν λ
Έλμ μμ λ
Έλμ΄μ΄μΌ νλ€. κ·Έλ μ§ μμΌλ©΄ DOMException μλ¬κ° λ°μνλ€. λ λ²μ§Έ μΈμλ‘ μ λ¬λ°μ λ
Έλκ° nullμ΄λ©΄ appendChild λ©μλμ²λΌ λμνλ€.DOMμ μ΄λ―Έ μ‘΄μ¬νλ λ Έλλ₯Ό appendChild λλ insertBefore λ©μλλ₯Ό μ΄μ©νμ¬ DOMμ λ€μ μΆκ°νλ©΄ νμ¬ μμΉμμ λ Έλλ₯Ό μ κ±°νκ³ μλ‘μ΄ μμΉμ λ Έλλ₯Ό μΆκ°νλ€. μ¦, λ Έλκ° μ΄λνλ€.
<!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>
**Node.prototype.cloneNode([deep: true | false]
)** λ©μλλ λ
Έλμ μ¬λ³Έμ μμ±νμ¬ λ°ννλ€. 맀κ°λ³μ deepμ trueλ₯Ό μΈμλ‘ μ λ¬νλ©΄ λ
Έλλ₯Ό κΉμ 볡μ¬νμ¬ λͺ¨λ μμ λ
Έλκ° ν¬ν¨λ μ¬λ³Έμ μμ±νκ³ , falseλ₯Ό μΈμλ‘ μ λ¬νκ±°λ μλ΅νλ©΄ λ
Έλλ₯Ό μμ λ³΅μ¬ νμ¬ λ
Έλ μμ λ§μ μ¬λ³Έμ μμ±νλ€. μμ 볡μ¬λ‘ μμ±λ μμ λ
Έλλ μμ λ
Έλλ₯Ό 볡μ¬νμ§ μμΌλ―λ‘ ν
μ€νΈ λ
Έλλ μλ€.
<!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>
**Node.prototype.replaceChild(newChild, oldChild)**
λ©μλλ μμ μ νΈμΆν λ
Έλμ μμ λ
Έλλ₯Ό λ€λ₯Έ λ
Έλλ‘ κ΅μ²΄νλ€. 첫 λ²μ§Έ 맀κ°λ³μ newChildμλ κ΅μ²΄ν μλ‘μ΄ λ
Έλλ₯Ό μΈμλ‘ μ λ¬νκ³ , λ λ²μ§Έ 맀κ°λ³μ oldChildμλ μ΄λ―Έ μ‘΄μ¬νλ κ΅μ²΄λ λ
Έλλ₯Ό μΈμλ‘ μ λ¬νλ€. oldChild 맀κ°λ³μμ μΈμλ‘ μ λ¬ν λ
Έλλ replaceChild λ©μλλ₯Ό νΈμΆν λ
Έλμ μμ λ
Έλμ΄μ΄μΌ νλ€.
μ¦, replaceChild λ©μλλ μμ μ νΈμΆν λ Έλμ μμ λ ΈλμΈ oldChild λ Έλλ₯Ό newChild λ Έλλ‘ κ΅μ²΄νλ€. μ΄λ oldChild λ Έλλ DOMμμ μ κ±°λλ€.
<!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>
**Node.prototype.removeChild(child)**
λ©μλλ child 맀κ°λ³μμ μΈμλ‘ μ λ¬ν λ
Έλλ₯Ό DOMμμ μμ νλ€. μΈμλ‘ μ λ¬ν λ
Έλλ removeChild λ©μλλ₯Ό νΈμΆν λ
Έλμ μμ λ
Έλμ΄μ΄μΌ νλ€.
<!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>
HTML λ¬Έμμ ꡬμ±μμμΈ HTML μμλ μ¬λ¬ κ°μ μ΄νΈλ¦¬λ·°νΈ(μμ±)λ₯Ό κ°μ§ μ μλ€.
<input id="user" type="text" value="ungmo2">
HTML λ¬Έμκ° νμ±λ λ HTML μμμ μ΄νΈλ¦¬λ·°νΈλ μ΄νΈλ¦¬λ·°νΈ λ Έλλ‘ λ³νλμ΄ μμ λ Έλμ μ°κ²°λλ€. μ΄λ HTML μ΄νΈλ¦¬λ·°νΈλΉ νλμ μ΄νΈλ¦¬λ·°νΈ λ Έλκ° μμ±λλ€. μ¦, μ input μμλ 3κ°μ μ΄νΈλ¦¬λ·°νΈκ° μμΌλ―λ‘ 3κ°μ μ΄νΈλ¦¬λ·°νΈ λ Έλκ° μμ±λλ€.
μ΄λ λͺ¨λ μ΄νΈλ¦¬λ·°νΈ λ Έλμ μ°Έμ‘°λ μ μ¬ λ°°μ΄ κ°μ²΄μ΄μ μ΄ν°λ¬λΈμΈ NameNodeMap κ°μ²΄μ λ΄κ²¨μ μμ λ Έλμ attributes νλ‘νΌν°μ μ μ₯λλ€.
HTML μ΄νΈλ¦¬λ·°νΈ κ°μ μ°Έμ‘°νλ €λ©΄ Element.prototype.getAttribute(attributeName)
κ°μ λ³κ²½νλ €λ©΄ Element.prototype.setAttribute(attributeName,attributeValue)
λ©μλλ₯Ό μ¬μ©νλ€.
<!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>
νΉμ HTML μ΄νΈλ¦¬λ·°νΈκ° μ‘΄μ¬νλμ§ νμΈνλ €λ©΄ Element.prototype.hasAttribute(attributeName)
, νΉμ HTML μ΄νΈλ¦¬λ·°νΈλ₯Ό μμ νλ €λ©΄ Element.prototype.removeAttribute(attributeName)
λ©μλλ₯Ό μ¬μ©νλ€.
<!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>
μμ λ Έλ κ°μ²΄μλ HTML μ΄νΈλ¦¬λ·°νΈμ λμνλ νλ‘νΌν°(μ΄ν DOM νλ‘νΌν°)κ° μ‘΄μ¬νλ€. μ΄ DOM νλ‘νΌν°λ€μ HTML μ΄νΈλ¦¬λ·°νΈ κ°μ μ΄κΈ°κ°μΌλ‘ κ°μ§κ³ μλ€.
HTML μ΄νΈλ¦¬λ·°νΈμ μν μ HTML μμμ μ΄κΈ° μνλ₯Ό μ§μ νλ κ²μ΄λ€. μ¦ HTML μ΄νΈλ¦¬λ·°νΈ κ°μ HTML μμμ μ΄κΈ° μνλ₯Ό μλ―Ένλ©° μ΄λ λ³νμ§ μλλ€.
μμ λ Έλλ μν(state)λ₯Ό κ°μ§κ³ μλ€. μμ λ Έλλ μ΄κΈ° μνμ μ΅μ μν μ¦, 2κ°μ μνλ₯Ό κ΄λ¦¬ν΄μΌ νλ€. μμ λ Έλμ μ΄κΈ° μνλ μ΄νΈλ¦¬λ·°νΈ λ ΈνΈκ° κ΄λ¦¬νλ©°, μμ λ Έλμ μ΅μ μνλ DOM νλ‘νΌν°κ° κ΄λ¦¬νλ€.
```jsx
<!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>
```
μ΄μ²λΌ **HTML μ΄νΈλ¦¬λ·°νΈλ HTML μμμ μ΄κΈ° μν κ°μ κ΄λ¦¬νκ³ DOM νλ‘νΌν°λ μ¬μ©μμ μ
λ ₯μ μν΄ λ³κ²½λλ μ΅μ μνλ₯Ό κ΄λ¦¬**νλ€. λ¨, λͺ¨λ DOM νλ‘νΌν°κ° μ¬μ©μμ μ
λ ₯μ μν΄ λ³κ²½ μ΅μ μνλ₯Ό κ΄λ¦¬νλκ²μ μλλ€**. μ¬μ©μμ μ
λ ₯μ μν μν λ³νμ κ΄κ³μλ DOM νλ‘νΌν°λ§ μ΅μ μν κ°μ κ΄λ¦¬**νλ€.
<!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>
data μ΄νΈλ¦¬λ·°νΈμ dataset νλ‘νΌν°λ₯Ό μ¬μ©νλ©΄ HTML μμμ μ μν μ¬μ©μ μ μ μ΄νΈλ¦¬λ·°νΈμ μλ°μ€ν¬λ¦½νΈ κ°μ λ°μ΄ν°λ₯Ό κ΅νν μ μλ€. data μ΄νΈλ¦¬λ·°νΈλ data-user-id, data-roleκ³Ό κ°μ΄ data- μ λμ¬ λ€μμ μμμ μ΄λ¦μ λΆμ¬ μ¬μ©νλ€.
data μ΄νΈλ¦¬λ·°νΈμ κ°μ HTMLElement.dataset νλ‘νΌν°λ‘ μ·¨λν μ μλ€. dataset νλ‘νΌν°λ HTML μμμ λͺ¨λ data μ΄νΈλ¦¬λ·°νΈμ μ 보λ₯Ό μ 곡νλ DOMStringMap κ°μ²΄λ₯Ό λ°ννλ€. DOMStringMap κ°μ²΄λ data μ΄νΈλ¦¬λ·°νΈμ data-μ λμ¬ λ€μμ λΆμΈ μμμ μ΄λ¦μ μΉ΄λ© μΌμ΄μ€λ‘ λ³νν νλ‘νΌν°λ₯Ό κ°μ§κ³ μλ€. μ΄ νλ‘νΌν°λ‘ data μ΄νΈλ¦¬λ·°νΈμ κ°μ μ·¨λνκ±°λ λ³κ²½ν μ μλ€.
<!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];
console.log(users)
// 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>
data μ΄νΈλ¦¬λ·°νΈμ data- μ λμ¬ λ€μμ μ‘΄μ¬νμ§ μλ μ΄λ¦μ ν€λ‘ μ¬μ©νμ¬ dataset νλ‘νΌν°μ κ°μ ν λΉνλ©΄ HTML μμμ data μ΄νΈλ¦¬λ·°νΈκ° μΆκ°λλ€. μ΄λ dataset νλ‘νΌν°μ μΆκ°ν μΉ΄λ©μΌμ΄μ€(fooBar)μ νλ‘νΌν° ν€λ data μ΄νΈλ¦¬λ·°νΈμ data- μ λμ¬ λ€μμ μΌλ°₯μΌμ΄μ€(data-foo-bar)λ‘ μλ λ³κ²½λμ΄ μΆκ°λλ€.
<!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>
HTMLElement.prototype.style
νλ‘νΌν°λ setterμ getter λͺ¨λ μ‘΄μ¬νλ μ κ·Όμ νλ‘νΌν°λ‘μ μμ λ
Έλμ μΈλΌμΈ μ€νμΌμ μ·¨λνκ±°λ μΆκ° λλ λ³κ²½νλ€.
<!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>
style νλ‘νΌν°λ₯Ό μ°Έμ‘°νλ©΄ CSSStyleDeclaration νμ μ κ°μ²΄λ₯Ό λ°ννλ€. CSSStyle.Declaration κ°μ²΄λ λ€μν CSS νλ‘νΌν°μ λμνλ νλ‘νΌν°λ₯Ό κ°μ§κ³ μμΌλ©°, μ΄ νλ‘νΌν°μ κ°μ ν λΉνλ©΄ ν΄λΉ CSSS νλ‘νΌν°κ° μΈλΌμΈ μ€νμΌλ‘ HTML μμμ μΆκ°λκ±°λ λ³κ²½λλ€.
CSS νλ‘νΌν°λ μΌλ°₯ μΌμ΄μ€λ₯Ό λ°λ₯Έλ€. μ΄μ λμνλ CSSStyleDeclarationκ°μ²΄μ νλ‘νΌν°λ μΉ΄λ© μΌμ΄μ€λ₯Ό λ°λ₯Έλ€. μλ₯Ό λ€μ΄, CSS νλ‘νΌν° background-colorμ λμνλ CSSStyleDeclaration κ°μ²΄μ νλ‘νΌν° backgroundColorμ΄λ€.
$div.style.backgroundColor = 'yellow';
$div.style['background-color'] = 'yellow';
$div.style.width = '100px';
μΌλ°₯ μΌμ΄μ€μ CSSνλ‘νΌν°λ₯Ό κ·Έλλ‘ μ¬μ©νλ €λ©΄ κ°μ²΄μ λ§μΉ¨ν νκΈ°λ² λμ λκ΄νΈ νκΈ°λ²μ μ¬μ©νλ€. λ¨μμ§μ μ΄ νμν CSS νλ‘νΌν°μ κ°μ λ°λμ λ¨μλ₯Ό μ§μ ν΄μΌ νλ€.
.μΌλ‘ μμνλ ν΄λμ€ μ νμλ₯Ό μ¬μ©νμ¬ CSS classλ₯Ό 미리 μ μν λ€μ, HTML μμμ class μ΄νΈλ¦¬λ·°νΈ κ°μ λ³κ²½νμ¬ HTML μμμ μ€νμΌμ λ³κ²½ν μλ μλ€. μ΄λ HTML μμμ class μ΄νΈλ¦¬λ·°νΈλ₯Ό μ‘°μνλ €λ©΄ ckass μ΄νΈλ¦¬λ·°νΈμ λμνλ μμ λ Έλμ DOM νλ‘νΌν°λ₯Ό μ¬μ©νλ€. λ¨, class μ΄νΈλ¦¬λ·°νΈμ λμνλ DOM νλ‘νΌν°λ classκ° μλλΌ classNameκ³Ό classListλ€. μλ°μ€ν¬λ¦½νΈμμ classλ μμ½μ΄κΈ° λλ¬Έμ΄λ€.
<!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>
className νλ‘νΌν°λ λ¬Έμμ΄μ λ°ννλ―λ‘ κ³΅λ°±μΌλ‘ ꡬλΆλ μ¬λ¬ κ°μ ν΄λμ€λ₯Ό λ°ννλ κ²½μ° λ€λ£¨κΈ°κ° λΆνΈνλ€.<!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>
DOMTokenList κ°μ²΄λ class μ΄νΈλ¦¬λ·°νΈμ μ 보λ₯Ό λνλ΄λ 컬λ μ
κ°μ²΄λ‘μ μ μ¬ λ°°μ΄ κ°μ²΄μ΄λ©΄μ μ΄ν°λ¬λΈμ΄λ€. DOMTokenList κ°μ²΄λ λ€μκ³Ό κ°μ΄ μ μ©ν λ©μλλ€μ μ 곡νλ€.$box.classList.add('foo'); // -> class="box red foo"
$box.classList.add('bar', 'baz'); // -> class="box red foo bar baz"
```jsx
$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"
λ λ²μ§Έ μΈμλ‘ λΆλ¦¬μΈ κ°μΌλ‘ νκ°λλ 쑰건μμ μ λ¬ν μ μλ€. μ΄λ 쑰건μμ νκ° κ²°κ³Όκ° trueμ΄λ©΄ class μ΄νΈλ¦¬λ·°νΈμ κ°μ λ‘ μ²« λ²μ§Έ μΈμλ‘ μ λ¬λ°μ λ¬Έμμ΄μ μΆκ°νκ³ , falseμ΄λ©΄ class μ΄νΈλ¦¬λ·°νΈμμ κ°μ λ‘ μ²« λ²μ¨° μΈμλ‘ μ λ¬λ°μ λ¬Έμμ΄μ μ κ±°νλ€.// class μ΄νΈλ¦¬λ·°νΈμ κ°μ λ‘ 'foo' ν΄λμ€λ₯Ό μΆκ°
$box.classList.toggle('foo', true); // -> class="box blue foo"
// class μ΄νΈλ¦¬λ·°νΈμμ κ°μ λ‘ 'foo' ν΄λμ€λ₯Ό μ κ±°
$box.classList.toggle('foo', false); // -> class="box blue"
style νλ‘νΌν°λ μΈλΌμΈ μ€νμΌλ§ λ°ννλ€. λ°λΌμ ν΄λμ€λ₯Ό μ μ©ν μ€νμΌμ΄λ μμμ ν΅ν΄ μ묡μ μΌλ‘ μ μ©λ μ€νμΌμ style νλ‘νΌν°λ‘ μ°Έμ‘°ν μ μλ€. HTML μμμ μ μ©λμ΄ μλ λͺ¨λ CSS μ€νμΌμ μ°Έμ‘°ν΄μΌ ν κ²½μ° getComputedStyle λ©μλλ₯Ό μ¬μ©νλ€.
window.getComputedStyle(element[, pseudo]) λ©μλλ 첫 λ²μ§Έ μΈμ(element)λ‘ μ λ¬ν μμ λ Έλμ μ μ©λμ΄ μλ νκ°λ μ€νμΌμ CSSStyleDeclaration κ°μ²΄μ λ΄μ λ°ννλ€.
<!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>
getComputedStyle λ©μλμ λ λ²μ§Έ μΈμ(pseudo)λ‘ :after, :beforeμ κ°μ μμ¬ μμλ₯Ό μ§μ νλ λ¬Έμμ΄μ μ λ¬ν μ μλ€. μμ¬ μμκ° μλ μΌλ° μμμ κ²½μ° λ λ²μ§Έ μΈμλ μλ΅νλ€.
<!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>
DOMμ νμ¬ λ€μκ³Ό κ°μ΄ 4κ°μ λ 벨(λ²μ )μ΄ μλ€.
λ 벨 | νμ€λ¬Έμ URL |
---|---|
DOM Level 1 | https://www.w3.org/TR/REC-DOM-Level-1 |
DOM Level 2 | https://www.w3.org/TR/DOM-Level-2-Core |
DOM Level 3 | https://www.w3.org/TR/DOM-Level-3-Core |
DOM Level 4 | https://dom.spec.whatwg.org |