[Javascript] ์๋ฐ์คํฌ๋ฆฝํธ์์ HTML DOM์ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ
์ค๋์ JS ์์ HTML DOM ๊ฐ์ฒด๋ฅผ ๊ฐ์ ธ์ ์กฐ์ํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณผ ๊ฒ์ด๋ค.
์ค๋ ๋ฐฐ์ธ ๋ด์ฉ๋ค์ด ๋ชจ๋ ์ํฉ์์ ์ฌ์ฉ๋๋ ๊ฒ์ ์๋๊ณ ๋์ถฉ JS ์์๋ ์ด๋ ๊ฒ ํ ์ ์๊ตฌ๋ ํ๋ ์๊ฐ์ด ๋ชฉ์ ์ด๋ค.
๋ ์์ธํ ๋ด์ฉ์ ๋ง์ด ์์ผ๋ ์ด๋ฒ ๊ธ์ ๋ณด๊ณ ๋ฐฉํฅ์ ์ก์๊ฐ๊ธธ ๋ฐ๋๋ค.
ํ์ต ์์
- ์๋ฆฌ๋จผํธ ๊ฐ์ ธ์ค๊ธฐ
getElementById()
getElementByName()
getElementByClassName()
getElementByTagName()
- ์๋ฆฌ๋จผํธ์ ์ํ๋ ํ
์คํธ ์ถ๊ฐํ๊ธฐ
innerHTML
innerText
- ์๋ฆฌ๋จผํธ ์์ฑ, ์ญ์ , ์์ ํ๊ธฐ
createElement()
appendChild()
removeChild()
์๋ฆฌ๋จผํธ ๊ฐ์ ธ์ค๊ธฐ
HTML ์์ (Elements)๋ฅผ ์ ์ดํ๋ ค๋ฉด ์ฐ์ HTML ํ์ผ์ ์๋ HTML ํ๊ทธ ์์๋ค์ JS ํ์ผ๋ก ๋ถ๋ฌ์์ผ ํ๋ค.
๊ทธ ๋ฐฉ๋ฒ์ ํฌ๊ฒ 4๊ฐ์ง๊ฐ ์กด์ฌํ๋ค.
- id ์์ฑ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
- HTML Tag ๋ก ๊ฐ์ ธ์ค๊ธฐ
- name ์์ฑ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
- class ์์ฑ๊ฐ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
id ์์ฑ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ & name ์์ฑ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello world</title>
</head>
<body>
<div id="first">์ฒซ ๋ฒ์งธ</div>
<div name="second">๋ ๋ฒ์งธ</div>
</body>
</html>
์์ ๊ฐ์ด div ํ๊ทธ๊ฐ id๋ก ์ง์ ๋์ด ์๋ค๋ฉด ์ฐ๋ฆฌ๋ getElementById()
๋ฉ์๋๋ฅผ ํตํด์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
ํน์ div ํ๊ทธ๊ฐ name์ผ๋ก ์ง์ ๋์ด ์๋ค๋ฉด getElementByName()
์ ์ฌ์ฉํ๋ค.
const element1 = document.getElementById("first");
const element2 = document.getElementByName("second");
HTML Tag ๋ก ๊ฐ์ ธ์ค๊ธฐ & Class ๋ช ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello world</title>
</head>
<body>
<div>hello</div>
<div>world</div>
<span class="card">์๋
ํ์ธ์ฉ</span>
<span class="card">๋ธ๋ก๊ทธ ์ฃผ์ธ์ด์์ฉ</span>
</body>
</html>
์์ ๊ฐ์ด div ํ๊ทธ๋ก ๊ตฌ์ฑ๋ ๋ชจ๋ ์๋ฆฌ๋จผํธ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ถ๋ค๋ฉด getElementByTagName()
๋ฉ์๋๋ฅผ ํตํด์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
๋ํ card๋ผ๋ classname์ ๊ฐ์ง ๋ชจ๋ ์๋ฆฌ๋จผํธ๋ฅผ ๊ฐ์ ธ์ค๊ณ ์ถ๋ค๋ฉด getElementByClassName()
๋ฉ์๋๋ฅผ ํตํด์ ๊ฐ์ ธ์จ๋ค.
ํน์ดํ ์ ์ getElementByTagName()
๊ณผ getElementByClassName()
๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค๋ฉด HTML Element ๊ฐ์ฒด ๋ฐฐ์ด์ ๊ฐ์ ธ์ค๋ฏ๋ก ๋ฐฐ์ด ์ ๊ทผ์ ํด์ผ ํ๋ค.
const elements1 = document.getElementByTagName("div");
const elements2 = document.getElementByClassName("card");
console.log(elements1[0]);
console.log(elements2[1]);
console.log(elements2[0]);
์๋ฆฌ๋จผํธ์ ์ํ๋ ํ ์คํธ๋ฅผ ์ฐ๊ณ ์ฝ๊ธฐ
HTML Element์ ์ํ๋ ํ ์คํธ๋ฅผ JS ์์ ์์ฑํ๊ณ ์ฃผ์ ํด์ค ์ ์์ผ๋ฉฐ ๋ํ HTML Element ์ ์๋ ๋ด์ฉ์ JS๋ก ์ฝ์ด์ฌ ์๋ ์๋ค.
- HTML Element Text ์ฝ๊ธฐ : textContent
- HTML Element Text ์ถ๊ฐํ๊ธฐ : innerHTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello world</title>
</head>
<body>
<div>hello</div>
<div>world</div>
<span id="first">์๋
ํ์ธ์ฉ</span>
<span id="second">๋ธ๋ก๊ทธ ์ฃผ์ธ์ด์์ฉ</span>
</body>
</html>
์์ ๊ฐ์ด ์กด์ฌํ๋ค๋ฉด ์ฐ๋ฆฌ๋ ์ ์ ๋ฐฐ์ด document.getElementBy..
๋ฅผ ํตํด์ Element๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ Element.textContent
์ Element.getElementById
์์ฑ์ ํตํด์ ํ
์คํธ ๊ฐ์ ์กฐ์ํ๋ฉด๋๋ค.
const first = document.getElementById("first");
const second = document.getElementById("second");
console.log(first.textContent); // ์๋
ํ์ธ์ฉ
second.innerHTML = second.textContent + " ๋ง๋์ ๋ฐ๊ฐ์์";
console.log(first.textContent); // ์๋
ํ์ธ์ฉ
console.log(second.textContent); // ๋ธ๋ก๊ทธ ์ฃผ์ธ์ด์์ฉ ๋ง๋์ ๋ฐ๊ฐ์์
๊ทธ๋ผ ๋ง์ฝ <input type="text" />
์ ์
๋ ฅ๋ ๊ฐ์ ๊ฐ์ ธ์ค๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
์ด๋ Element.value
๋ก ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
์๋ฆฌ๋จผํธ ์์ฑ, ์ฝ์ , ์ญ์ ํ๊ธฐ
์ฐ๋ฆฌ๋ JS์์ DOM ๋ ธ๋ ๊ฐ์ฒด ์ฆ ์๋ฆฌ๋จผํธ๋ฅผ ๋ง๋ค๊ณ DOM ํธ๋ฆฌ์ ์ฝ์ ํ๊ฑฐ๋ ์ญ์ ํ๊ฑฐ๋ ์นํํ ์ ์๋ค.
- ๋ ธ๋ ์์ฑํ๊ธฐ
- ๋ ธ๋ ์ฝ์ ํ๊ธฐ
- ๋ ธ๋ ์ญ์ ํ๊ธฐ
๋ ธ๋ ์์ฑํ๊ธฐ
์๋ก์ด Element ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด createElement
๋ฉ์๋๋ฅผ ์์ฑํ๋ค.
const newNode = document.createElement("h1");
const newNode = document.createElement("li");
const newNode = document.createElement("href");
๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋๋ฐ, ์ฌ์ฉ๋ฒ์ ์๋ ๋ ธ๋ ์ฝ์ ํ๊ธฐ์ ํจ๊ป ํด๋ณด๊ฒ ๋ค.
๋ ธ๋ ์ฝ์ ํ๊ธฐ
๋ ธ๋๋ฅผ ์ฝ์ ํ๊ธฐ ์ํด์๋ ๋ ๊ฐ์ง ๋ฉ์๋๊ฐ ์ฌ์ฉ๋ ์ ์๋ค.
ํ์ง๋ง ์ด๋ฒ ์๊ฐ์ ๋ชฉ์ ์ HTML DOM์ JS์์ ์กฐ์ํ ์ ์๋ค๋ ๊ฒ์ ์๊ธฐ ๋๋ฌธ์, ์์ธํ๊ฒ ๋ค์ด๊ฐ์ง ์๊ณ ์์์ ๋ง์ง๋ง์ ์ฝ์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด์๋ง ์์๋ณผ ๊ฒ์ด๋ค.
์์์ ๋ง์ง๋ง์ ์ฝ์
: Node.appendChild()
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello world</title>
</head>
<body>
<div>hello</div>
<div>world</div>
<span id="first">์๋
ํ์ธ์ฉ</span>
<span id="second">๋ธ๋ก๊ทธ ์ฃผ์ธ์ด์์ฉ</span>
</body>
</html>
const first = document.getElementById("first");
const newNode = document.createElement("span");
newNode.innerHTML = "๋ง๋์ ๋ฐ๊ฐ์ต๋๋ค";
first.appendChild(newNode);
๋ ธ๋ ์ญ์ ํ๊ธฐ
๋
ธ๋๋ฅผ ์ญ์ ํ ๋๋ removeChild()
๋ฅผ ์ฌ์ฉํ๋ค.
์ด๋ฆ์์๋ ์ ์ ์๋ฏ, ์์ ํ๊ทธ๋ฅผ ์ญ์ ํ๋๋ฐ ์ญ์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๋ถ๋ชจํ๊ทธ์์ ์ฌ์ฉํด์ผ ํ๋ค.
node.parentNode.removeChild(node);