- HTML ๋ฌธ๋ฒ์ผ๋ก ์์ฑํ ํ๊ทธ, ์ฃผ์, ํ ์คํธ์ ๊ฐ์ ๊ตฌ์ฑ ์์๋ค์ ์น๋ธ๋ผ์ฐ์ ์์ ๊ฐ๊ฐ ํ๋์ ๊ฐ์ฒด๋ก ์ธ์ ํฉ๋๋ค.
- ์ด๋ฌํ HTML ๊ตฌ์ฑ ์์๋ค์ ๋ค๋ฃจ๋ ๊ฐ์ฒด๋ฅผ ๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ(DOM)์ด๋ผ๊ณ ํฉ๋๋ค.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document Object Model</title>
</head>
<body>
<h1>header</h1>
<a href="#">link</a>
</body>
</html>
- DOM ํธ๋ฆฌ๋ document ๊ฐ์ฒด ํ์์ HTML ํ๊ทธ ์์, ์์ฑ, ํ ์คํธ, ์ฃผ์ ๋ฑ์ด 'ํธ๋ฆฌ' ํํ๋ก ๊ตฌ์ฑ๋๋๋ฐ, ์ด๋ค์ ๊ฐ๊ฐ '๋ ธ๋'๋ผ๊ณ ํฉ๋๋ค.
- document๋ ๋ ธ๋๊ฐ ์๋๋ผ ๊ฐ์ฒด์ด๋ฏ๋ก ์ฌ๊ธฐ์๋ html์ด ๋ฃจํธ ๋ ธ๋๊ฐ ๋ฉ๋๋ค.
๋ฉ์๋๋ก ๋ ธ๋ ์ ํํ๊ธฐ
- ์ผ๋ฐ์ ์ผ๋ก ์์ ๋ ธ๋๋ฅผ ๋ฐ๋ก ์ ํํ ์ ์๋ ๋ฉ์๋๋ฅผ ์ด์ฉํ ๋ ธ๋ ์ ํ ๋ฐฉ๋ฒ์ด ๋ง์ด ์ฐ์ ๋๋ค.
์์ฑ๊ฐ๊ณผ ํ๊ทธ๋ช ์ฌ์ฉ - get ๋ฉ์๋
getElementById("id๋ช ")
- getElementById() ๋ฉ์๋๋ก ์ ํํ๋ ์์ ๋ ธ๋๋ ๋ฌด์กฐ๊ฑด 1๊ฐ์ด๊ธฐ ๋๋ฌธ์ ํด๋น ์์ ํ๋๋ง ๋ณด์ฌ์ค๋๋ค.
<h1 id="title">์ ๋ชฉ ์
๋๋ค.</h1>
const el = document.getElementById("title");
console.log(el); // <h1 id="title">์ ๋ชฉ ์
๋๋ค.</h1>
getElementByclassName("class๋ช ")
- HTMLCollection ๊ฐ์ฒด๋ก ์ฌ๋ฌ ์์๋ฅผ ํ๊บผ๋ฒ์ ์ ํํฉ๋๋ค.
- HTMLCollection ๊ฐ์ฒด๋ ์ ์ฌ ๋ฐฐ์ด์ด๋ผ์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ก ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํฉ๋๋ค.
<p class="text">๋ณธ๋ฌธ ๋ด์ฉ ์
๋๋ค.</p>
<p class="text">๋ณธ๋ฌธ ๋ด์ฉ ์
๋๋ค.</p>
const classEl = document.getElementsByClassName("text");
console.log(classEl);
/* 0: p.text
1: p.text
length: 2 */
console.log(classEl[0]); // <p class="text">๋ณธ๋ฌธ ๋ด์ฉ ์
๋๋ค.</p>
for(let el of classEl) console.log(el); // ํฅ์๋ for๋ฌธ๊ณผ ์ ์ฌ
getElementByTagName("ํ๊ทธ๋ช ")
- HTMLCollection ๊ฐ์ฒด๋ก ์ฌ๋ฌ ์์๋ฅผ ํ๊บผ๋ฒ์ ์ ํ๊ฐ๋ฅํ๊ณ , ํ๊ทธ๋ช ๊ณผ ์ผ์นํ๋ ๋ ธ๋๋ฅผ ์ ํํฉ๋๋ค.
<p class="text">๋ณธ๋ฌธ ๋ด์ฉ ์
๋๋ค.</p>
<p class="text">๋ณธ๋ฌธ ๋ด์ฉ ์
๋๋ค.</p>
const tagEl = document.getElementsByTagName("p");
for(let e of tagEl) console.log(e);
CSS ์ ํ์ ์ฌ์ฉํ๊ธฐ - query ๋ฉ์๋
querySelector("์ ํ์") : 1๊ฐ๋ง
<div class="box-1">
<p class="text">text-1</p>
<p class="text">text-2</p>
</div>
const qEl = document.querySelector(".box-1");
console.log(qEl);
const qEls = document.querySelector(".box-1 .text");
console.log(qEls);
querySelectorAll("์ ํ์") : ๋ชจ๋ ์ ํ
- ์์ ๋ ธ๋ ์ ํํ ๋๋ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ก ํ ๊ฒ
const qEls2 = document.querySelectorAll(".box-1 .text");
console.log(qEls2);
console.log(qEls2[0]);
document ๊ฐ์ฒด์ get ๋ฉ์๋์ query ๋ฉ์๋๋ ๋ ๋ค ์ํ๋ ์์ ๋ ธ๋๋ฅผ ์ ํํ๋ค๋ ์ ์ ๊ฐ์ต๋๋ค.
ํ์ง๋ง query ๋ฉ์๋๋ ๋งค๊ฐ๋ณ์๋ก CSS ์ ํ์๋ฅผ ์ ๋ฌ๋ฐ๊ธฐ ๋๋ฌธ์ get ๋ฉ์๋๋ณด๋ค ๋ฒ์ฉ์ฑ์ด ๋ ์ข์ต๋๋ค.
๋ ธ๋ ์กฐ์ํ๊ธฐ
- document ๊ฐ์ฒด์ ์์ฑ์ด๋ ๋ฉ์๋๋ก ๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ์ ๋ ธ๋๋ฅผ ์ ํํ๊ณ ๋๋ฉด ์ ํํ ๋ ธ๋์ ์ฌ๋ฌ ์กฐ์์ ํ ์ ์์ต๋๋ค.
์ปจํ ์ธ ์กฐ์ํ๊ธฐ
๐ ์ ํํ ๋ ธ๋์ ํ์ ์ด ์์ ๋ ธ๋(h1, p, ...)๋ผ๋ฉด ๋ค์ ํ์ ์ ์๋ ์์ฑ์ ์ฌ์ฉํด ์ปจํ ์ธ ๋ฅผ ์กฐ์ํ ์ ์์ต๋๋ค.
textContent : ์์ ๋ ธ๋์ ๋ชจ๋ ํ ์คํธ์ ์ ๊ทผ
innerText : ์์ ๋ ธ๋์ ํ ์คํธ ์ค ์น ๋ธ๋ผ์ฐ์ ์ ํ์๋๋ ํ ์คํธ์ ์ ๊ทผ
innerHTML : ์์ ๋ ธ๋์ ํ ์คํธ ์ค HTML ํ๊ทธ๋ฅผ ํฌํจํ ํ ์คํธ ์ ๊ทผ
<p id="title2">Hello, <span style="display: none;">JavaScript!!!</span></p>
let txtContent = document.getElementById("title2").textContent;
let inText = document.getElementById("title2").innerText;
let inHTML = document.getElementById("title2").innerHTML;
console.log(txtContent); // Hello, JavaScript!!!
console.log(inText); // Hello,
console.log(inHTML); // Hello, <span style="display: none;">JavaScript!!!</span>
document.getElementById("title2").innerHTML = "<h1>Rest</h1>"; // ์ง์ html ์กฐ์ ๊ฐ๋ฅ
innerHTML ์์ฑ์ ๊ฐ์ ํ ๋นํ๋ฉด ํ๊ทธ๋ก ์ธ์ํด ๋ ธ๋์ ์ปจํ ์ธ ์ ์ ์ฉํฉ๋๋ค.
๊ทธ๋ฌ๋ textContent ์์ฑ๊ณผ innerHTML ์์ฑ์ ๋จ์ํ ํ ์คํธ๋ก ์ทจ๊ธํด์ ๊ฐ์ ๊ทธ๋๋ก ๋ ธ๋์ ์ปจํ ์ธ ์ ๋ฃ์ต๋๋ค.
์คํ์ผ ์กฐ์ํ๊ธฐ
const sEl = document.querySelectorAll(".text");
for(e of sEl) {
e.style.backgroundColor = "salmon";
e.style.fontSize = "50px";
e.style.color = "fff";
e.style.fontWeight = "bold";
}
- ์ ํ๋ ๋ ธ๋์ ํ์ ์ด ์์ ๋ ธ๋๋ผ๋ฉด style ์์ฑ์ผ๋ก ์์์ ์คํ์ผ(CSS)์ ์ง์ ํ ์ ์์ต๋๋ค.
ํด๋์ค ์์ฑ ์กฐ์ํ๊ธฐ
- style ์์ฑ์ผ๋ก ์คํ์ผ์ ์กฐ์ํ๋ฉด ์์ฑ์ ํ๋์ฉ ์ ์ด์ค์ผ ํด์ ๋ถํธํฉ๋๋ค.
- ์๋ฐ์คํฌ๋ฆฝํธ๋ class ์์ฑ์ ์กฐ์ํด ์คํ์ผ์ ์ ์ฉํ ์ ์์ต๋๋ค.
- ์ ํํ ์์ ๋ ธ๋์ class ์์ฑ์ ์ง์ ํ ๋๋ classList ์์ฑ์ add(), remove(), toggle() ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.active {
font-size: 2em;
font-weight: bold;
line-height: 1px;
}
.red-color {
color: red;
}
.blue-color {
color: blue;
}
</style>
</head>
<body>
<p id="text">text</p>
<a href="https://google.co.kr">๊ตฌ๊ธ</a>
<script src="./230330_2_DOM2.js"></script>
</body>
</html>
const el = document.querySelector("#text");
el.classList.add("active", "blue-color"); // class ์์ฑ ์ถ๊ฐ
el.classList.remove("active"); // class ์์ฑ ์ ๊ฑฐ
๋ฉ์๋ ์์ฑ์ผ๋ก ์กฐ์ํ๊ธฐ
- ๋ชจ๋ ์์ฑ์ ์ ์ฒด์ ์ผ๋ก ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
.getAttribute("์์ฑ๋ช ") : ์์ฑ๊ฐ์ ๊ฐ์ ธ์ด
.setAttribute("์์ฑ๋ช ", "์์ฑ๊ฐ") : ์์ฑ๊ฐ ์ค์
.removeAttribute("์์ฑ๋ช ") : ์์ฑ ์ญ์
const aEl = document.querySelector("a"); // a ํ๊ทธ ์ ํ
const href = aEl.getAttribute("href");
console.log(href); // https://google.co.kr
aEl.setAttribute("href", "https://kakao.com");
aEl.innerText = "์นด์นด์ค";
๋ ธ๋ ์ถ๊ฐ/์ญ์ ํ๊ธฐ
- DOM ํธ๋ฆฌ์์ ์๋ก์ด ๋ ธ๋๋ฅผ ์์ฑํ๊ณ , ์์ฑํ ๋ ธ๋๋ฅผ ๊ธฐ์กด DOM ํธ๋ฆฌ์ ์ฐ๊ฒฐํ๋ฉด ๋์ ์ผ๋ก ์๋ก์ด ์์๋ฅผ ํ๋ฉด์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
- ์์) ํฌ๋๋ฆฌ์คํธ, ํ ์ผ ๊ด๋ฆฌ ๋ฑ์ ์ด์ฉํ ์ ์์ต๋๋ค.
๋ ธ๋ ์ถ๊ฐํ๊ธฐ
- ์๋ก์ด ์์๋ฅผ ํ๋ฉด์ ์ถ๊ฐํ๋ ค๋ฉด ๋จผ์ DOM ํธ๋ฆฌ์ ์๋ก์ด ๋ ธ๋๋ฅผ ์์ฑํ๋ ์์ ์ ํด์ผ ํฉ๋๋ค.
- ๊ทธ๋ฆฌ๊ณ ์์ฑํ ๋ ธ๋๋ฅผ ๊ธฐ์กด์ DOM ํธ๋ฆฌ ๋ ธ๋์ ์ฐ๊ฒฐ ํฉ๋๋ค.
// createElement() : ์์ ๋
ธ๋ ์์ฑ
const aEl = document.createElement("div");
// <๊ธฐ์ค๋
ธ๋>.appendChild(์ถ๊ฐํ ์์ ๋
ธ๋);
document.body.appendChild(aEl);
aEl.innerText = "Test";
๋ ธ๋ ์ญ์ ํ๊ธฐ
๋ค์ ๋์ฌ 'ํฌ๋๋ฆฌ์คํธ' ์์ ์์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค.
ํผ ์กฐ์ํ๊ธฐ
form ํ๊ทธ ์ ํํ๊ธฐ
- HTML ํผ ์์์ ์์์ ํญ์ form ํ๊ทธ ์ ๋๋ค.
- ํผ ์์์๋ input, select, button ํ๊ทธ ๋ฑ์ด ์์ต๋๋ค. ์ด๋ฐ ํผ ์์๋ฅผ ์ ํํ ๋๋ elements ์์ฑ์ด๋ name ์์ฑ์ ์ฌ์ฉํฉ๋๋ค.
- ํผ ์์๋ ์ฌ์ฉ์์๊ฒ ๊ฐ์ ์ ๋ ฅ ๋ฐ์ ์๋ฒ์ ์ ๋ฌ ํฉ๋๋ค. ์ ๋ฌ ๋ฐ์ ๊ฐ์ ์๋ฒ์์ ์ด๋ป๊ฒ ํ์ฉํ๋์ง๋ ๋ฐฑ์๋ ์์ญ์ด์ง๋ง, ์๋ฒ์ ์ ๋ฌํ๊ธฐ๊น์ง ํผ ์์์ ์ ๋ ฅ๋ ๊ฐ์ด ์ ํจํ์ง ๊ฒ์ฆํ ์ ์์ด์ผ ํฉ๋๋ค.
- ์ฌ์ฉ์์๊ฒ ๊ฐ์ ์ ๋ ฅ๋ฐ๋ ๋ํ์ ์ธ ํผ ์์๋ก๋ input๊ณผ select๊ฐ ์์ต๋๋ค.
input(text,password,number,url,search,email)
- ์ด๋ฌํ ์์๋ค์ ํ ์ค ์์๋ค์ด๋ผ๊ณ ํฉ๋๋ค. ์ด๋ค์ ๊ฐ์ ๊ฐ์ ธ์ค๊ฑฐ๋ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ด ๋ชจ๋ ๋๊ฐ์ต๋๋ค.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>input set</title>
</head>
<body>
<form name="frm">
<input type="text" name="id">
<input type="password" name="pw">
</form>
<script>
document.frm.id.value = '๊ณฐ๋์ด์ฌ์ก์ฌ';
document.frm.pw.value = 'sphb8250';
</script>
</body>
</html>
textarea : ์ฌ๋ฌ ์ค ์ ๋ ฅ ์์
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>textarea</title>
</head>
<body>
<form name="frm">
<textarea name="desc"></textarea>
</form>
<script>
document.frm.desc.value = 'setting!';
</script>
</body>
</html>
์ฒดํฌ๋ฐ์ค ๋ค๋ฃจ๊ธฐ
- ์ฒดํฌ๋ฐ์ค๋ value ์์ฑ์ผ๋ก ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
- ํ์ง๋ง ํ ์ค ์ ๋ ฅ ์์์๋ ๋ค๋ฅด๊ฒ ์ฒดํฌ๋ฐ์ค์ ์ฒดํฌ ํ์๊ฐ ์๋ ๊ฒ๋ง ๊ฐ์ ธ์์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
์ฌ๊ธฐ์ ๋ค์ ํ ๋ฒ ์ง๊ณ ๋์ด๊ฐ๊ธฐ!
querySelectorAll์ DOM์์ ์ฌ์ฉ๋๋ ๋ฉ์๋ ์ค ํ๋๋ก, ํน์ ์ ํ์(selector)์ ์ผ์นํ๋ ๋ชจ๋ ์์(element)๋ฅผ ์ฐพ์ NodeList ํํ๋ก ๋ฐํํฉ๋๋ค.
const checkEl = document.querySelectorAll("[type='checkbox']");
for(let e of checkEl) {
if(e.value === "orange") e.checked = true;
}
์ฝค๋ณด ๋ฐ์ค ๋ค๋ฃจ๊ธฐ
const optionEl = document.querySelectorAll("option");
for(let e of optionEl) {
if(e.value === "orange") e.selected = true;
}
ํ์ผ ์ ๋ก๋ ๋ค๋ฃจ๊ธฐ
function upload() {
const filePath = document.frm.upload.files;
console.log(filePath);
}
<form name="frm">
<input type="file" name="upload">
</form>
<button onclick="upload()">ํ์ผ ์
๋ก๋</button>
์ค์ต ์์ - ํฌ๋๋ฆฌ์คํธ
- ์ ๋ ฅ ํ๋๋ฅผ ํตํด ๊ณผ๋ชฉ์ ์ ๋ ฅ ๋ฐ์์ ๋ฆฌ์คํธ ์ถ๊ฐํ๊ธฐ
- ๋ฆฌ์คํธ์ ์ถ๊ฐ๋ ํญ๋ชฉ ํด๋ฆญ์ ํด๋น ๋ฆฌ์คํธ ์ทจ์์ ๊ธ๊ธฐ
โฆ addEventListener() ๋ฉ์๋๋ ์ด๋ฒคํธ(event)๋ฅผ ์์์ ๋ฑ๋กํ๋ ๋ฉ์๋ ์ ๋๋ค. ์ด ๋ฉ์๋๋ ์ด๋ฒคํธ ์ ํ๊ณผ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ ์คํํ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ธ์๋ก ๋ฐ์ต๋๋ค.
โฆ click()์ ์ด๋ฒคํธ ์ ํ ์ค ํ๋์ด๋ฉฐ, ์ด ๊ฒฝ์ฐ ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ๋ฑ๋ก๋ ์ฝ๋ฐฑ ํจ์๊ฐ ์คํ๋ฉ๋๋ค. ์ด ์ฝ๋ฐฑ ํจ์๋ ์ต๋ช ํจ์๋ก ์์ฑ๋์์ผ๋ฉฐ, ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋ ์คํ๋ฉ๋๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
li {
list-style:none;
cursor: pointer;
}
li.completed {
text-decoration: line-through;
}
</style>
</head>
<body>
<h1>๐ฅDEV STUDY TO-DO List๐ฅ</h1>
<p>๊ณต๋ถํ ๊ณผ๋ชฉ์ ์ถ๊ฐํด๋ณด์ธ์.</p>
<p>๊ณต๋ถ๊ฐ ๋๋ ๊ณผ๋ชฉ์ ํด๋ฆญํ์ธ์.</p>
<label for="stydy">๊ณผ๋ชฉ๋ช
๐</label>
<input type="text" id="study">
<button onclick="addStudy()">์ถ๊ฐํ๊ธฐ</button>
<div id="planner"></div>
<script src="./230330_5_1_์คํฐ๋ํ๋๋.js"></script>
</body>
</html>
function addStudy() {
let ul = document.createElement("ul");
let study = document.getElementById("study").value;
let li = document.createElement("li");
li.innerText = study;
li.onclick = function() {
li.classList.toggle("completed");
}
ul.appendChild(li);
let studyEl = document.querySelector("#planner");
studyEl.appendChild(ul);
}
์ค์ต ์์ - ํฌ๋๋ฆฌ์คํธ(๊ฐ์ฌ๋)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="./230331_1_ToDoList_๊ฐ์ฌ๋.css">
</head>
<body>
<div id="container">
<h1>To Do List๐</h1>
<p>๊ณต๋ถํ ๊ณผ๋ชฉ์ ๊ธฐ๋กํด ๋ณด์ธ์.</p>
<p>๊ณต๋ถ๊ฐ ๋๋ ๊ณผ๋ชฉ์ ํด๋ฆญํด์ ์ญ์ ํ ์ ์์ต๋๋ค.</p>
<form action="">
<input type="text" id="subject" autofocus>
<button onclick="newRegister(); return false;">์ถ๊ฐํ๊ธฐ</button>
</form>
<ul id="itemList"></ul>
</div>
<script src="./230331_1_ToDoList_๊ฐ์ฌ๋.js"></script>
</body>
</html>
* {
box-sizing: border-box;
}
#container {
width: 500px;
margin: 20px auto;
padding: 20px;
}
input[type="text"] {
width: 370px;
float: left;
height: 30px;
padding-left: 30px;
}
button {
width: 90px;
height: 30px;
float: right;
background: rgb(2,0,36);
background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
color: #fff;
border: none;
}
ul {
padding-top: 50px;
list-style: none;
}
li {
line-height: 2; /* ํฐํธ ํฌ๊ธฐ์ 2๋ฐฐ */
}
li:hover {
cursor: pointer;
}ในใฑใ
ใ
function newRegister() {
// ulํ๊ทธ ์๋์ ์ถ๊ฐํ ์์ ๋
ธ๋ ์์ฑ
let newItem = document.createElement("li");
// ์ฌ์ฉ์๊ฐ input์ ์
๋ ฅํ ๊ฐ ๋ด๊ธฐ
let subject = document.querySelector("#subject");
// html ๋ฌธ์์ ์๋ก์ด ํ
์คํธ ๋
ธ๋๋ฅผ ๋ง๋ฌ.
let newText = document.createTextNode(subject.value);
// ํ
์คํธ ๋
ธ๋๋ฅผ ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก ์ถ๊ฐ
newItem.appendChild(newText);
let itemList = document.querySelector("#itemList");
// insertBefore() ๋ฉ์๋๋ DOM ์์๋ฅผ ํน์ ์์ ์ด์ ์ ์ฝ์
ํฉ๋๋ค.
// ์์ ๋
ธ๋ ์ค ์ฒซ๋ฒ์งธ ๋
ธ๋ ์์ ์ถ๊ฐ
itemList.insertBefore(newItem, itemList.childNodes[0]);
// ์
๋ ฅํ๋ฉด ์นธ ๋น์์ค
subject.value = "";
// ๋ฒํผ ๋๋ฅธ ๋ค input(#subject)์ ์๋์ผ๋ก ํฌ์ปค์ค ๊ฐ๋๋ก..
document.getElementById("subject").focus();
let items = document.querySelectorAll("li");
for(let e of items) {
e.addEventListener("click", function() {
// ์ฌ๊ธฐ์์ this๋ ํด๋ฆญํ ์์ ๊ทธ ์์ฒด
if(this.parentNode) { // ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์์์ ๋ถ๋ชจ๊ฐ ์์ผ๋ฉด ..
this.parentNode.removeChild(this);
}
});
}
}
์ถ๊ฐ๋ ์ฌํญ!
- ์คํํ์ ๋, ์ถ๊ฐํ๊ธฐ ๋๋ ์ ๋ input์ ์๋ ์ปค์
- ์ ๋ ฅํ๋ฉด ์นธ ์๋ ๋น์์ค
- ์ต๊ทผ์ ๋ฑ๋กํ๊ฒ ๋งจ ์๋ก ์ฌ๋ผ์ค๊ฒ ํ๊ธฐ