โ ๋ชฉํ
- ํ ์คํธ ํ๋ ๊ฐ์ ๊ฐ์ ์ ๋ ฅํ๋ฉด ์๋ li์ ๋ด์ฉ์ด ์ถ๊ฐ๋๋ค.
- ์ถ๊ฐ๋ li๋ฅผ ํด๋ฆญํ๋ฉด '์ญ์ ํ์๊ฒ ์ต๋๊น?' confrim ์ฐฝ์ด ๋จ๊ณ 'ํ์ธ'์ ๋๋ฅด๋ฉด ์ญ์ ๋๋ค.
#appendChild #createTextNode # createElement #์ด๋ฒคํธ์์
โ Html
<body>
<h1>Web Programming</h1>
<p>๊ณต๋ถํ ์ฃผ์ ๋ฅผ ๊ธฐ๋กํด ๋ณด์ธ์</p>
<label for="input">์ฃผ์ </label>
<input id="input" type="text" autofocus>
<button class="button">์ถ๊ฐ</button>
<hr>
<ul class="itemList">
<li>์ถ๊ฐํ ๊ฒ์</li>
</ul>
</body>
label์ for๊ณผ input์ id๋ฅผ ๋ง์ถฐ์ค๋๋ค.
label์ for ์์ฑ์ ํด๋น ๋ ์ด๋ธ์ด ์ด๋ค ์ ๋ ฅ ์์์ ์ฐ๊ฒฐ๋์ด ์๋์ง ์๋ ค์ค๋๋ค.
์ฌ์ฉ์๊ฐ label์ ํด๋ฆญํ๋ฉด ํด๋น ์ ๋ ฅ ํ๋์ ํฌ์ปค์ค๊ฐ ๊ฐ๊ฒ ๋ฉ๋๋ค.
๋๋ฌธ์ ํนํ ๋ชจ๋ฐ์ผ ๊ธฐ๊ธฐ์์ ์ ๋ ฅ ํ๋๋ณด๋ค ๋ ์ด๋ธ์ ํด๋ฆญํ์ฌ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ๊ธฐ ๋ ํธ๋ฆฌํด์ง๋๋ค.
label์ for์ ์ฌ์ฉํ๋ค๋ฉด ์ ๋ ฅ ์์์ id์ ๋์ผํ๊ฒ ๋ง์ถฐ์ผ ํฉ๋๋ค.
โ JavaScript - ํ ์คํธ ์ ๋ ฅ๊ฐ์ li์ ์ถ๊ฐ
<script>
const button = document.querySelector('.button');
const input = document.querySelector('#input');
const itemList = document.querySelector(".itemList");
document.addEventListener('DOMContentLoaded', function(){
button.addEventListener('click', function(e){
e.preventDefault();
var msg = input.value;
if(msg !== ""){
newRegister(msg);
// ์
๋ ฅ ํ ํ
์คํธ ํ๋ ๊ฐ์ ๋น์์ค
input.value = "";
}
});
});
function newRegister(msg){
// ์์๋ฅผ ๋ง๋ค๊ณ
const newItem = document.createElement('li');
// ์์์ text ์ถ๊ฐ
const newText= document.createTextNode(msg);
// ์์์ ํ
์คํธ๋ฅผ ๋ฃ์ด์ฃผ๊ณ
newItem.appendChild(newText);
// ์ ์ฒด ๋ฆฌ์คํธ์ ์์ฑ๋ li๋ฅผ ๋ฃ์ด์ค๋ค
itemList.appendChild(newItem);
}
</script>
- ์ํฐ ํน์ '์ถ๊ฐ' ๋ฒํผ์ ๋๋ฅด๋ฉด
- ํ ์คํธ ํ๋์ ์ ๋ ฅ ๋ฐ์ ๊ฐ์ ์๋ li์ ๋งจ ์๋๋ถํฐ ์ถ๊ฐ๋ฉ๋๋ค
์์๊ฐ ์ค์ํฉ๋๋ค.
1. ์์๋ฅผ ๋ง๋ค๊ณ
์๋ก์ด 'li'๋ฅผ ์ถ๊ฐํ๊ธฐ ์ํด createElement ํฉ๋๋ค.
const newItem = document.createElement('li');
2. text๋ฅผ ๋ง๋ค๊ณ
์๋ก์ด TextNode๋ฅผ ๋ง๋ค๊ธฐ ์ํด msg์ ๊ฐ์ผ๋ก textNode๋ฅผ ๋ง๋ญ๋๋ค.
const newText= document.createTextNode(msg);
3. ์์์ text๋ฅผ ๋ฃ๊ณ
์๋กญ๊ฒ ๋ง๋ 'li'์ msg ๊ฐ์ธ textNode๋ฅผ ์ถ๊ฐํฉ๋๋ค.
newItem.appendChild(newText);
4. ์ ์ฒด ๋ฆฌ์คํธ์ ์์๋ฅผ ๋ฃ๋๋ค.
์ต์๋จ ul (์ ์ฒด ๋ฆฌ์คํธ) ์ ์์ฑ๋ li๋ฅผ ๋ฃ์ด์ค๋๋ค.
itemList.appendChild(newItem);
โ JavaScript - li๋ฅผ ํด๋ฆญํด์ ์ญ์ ํ๊ธฐ
์ด์ ์ถ๊ฐ๋ li๋ฅผ ํด๋ฆญํ๋ฉด ์ญ์ ๊ฐ ๋ฉ๋๋ค.
๊ทธ๋ ๋ค๋ฉด ๊ฐ๊ฐ์ li์ ๋ชจ๋ ๋ฆฌ์ค๋๋ฅผ ๋ฌ์์ผ ํ ๊น์?
li๊ฐ ์์ฃผ ๋ง์์ง๋ค๋ฉด ์ด๋ ต์ต๋๋ค.
์ด๋ด ๋ '์ด๋ฒคํธ ์์'์ ์ฌ์ฉํฉ๋๋ค.
๐ก ์ด๋ฒคํธ ์์ ๐ก
์ด๋ฒคํธ ์์์ด๋ ๋ถ๋ชจ ์์์ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ํ๋๋ง ์ถ๊ฐํ๊ณ ๋ชจ๋ ์์ ์์์ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
์ค์ ๋ก ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋์์ด event ๊ฐ์ฒด์ target ํ๋กํผํฐ (event.target)์ธ์ง ํ์ธํ๋ฉด ๋ฉ๋๋ค.
itemList.addEventListener('click', (e) => {
if(e.target.tagName === 'LI'){
if(confirm("์ญ์ ํ์๊ฒ ์ต๋๊น?")){
e.target.remove();
};
}
});
- ์ ์ฒด ๋ฆฌ์คํธ์ธ ul itemList๋ฅผ ํด๋ฆญํ ํ
- ํด๋ฆญํ ํ๊ฒ์ tagName์ด 'LI'์ธ์ง ํ์ธํฉ๋๋ค.
์ด๋ ํ๊ทธ ์ด๋ฆ์ ๋๋ฌธ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์ดํ, confrim ์ฐฝ์์ ๋ฐํ ๊ฐ์ด true ์ฆ, ํ์ธ์ ๋๋ ๋ค๋ฉด ํด๋น target์ ์ญ์ ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
โ ์ ์ฒด ์ฝ๋
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>์
๋ ฅ๊ฐ ๋ฐ์ ์ถ๊ฐํ๊ธฐ</title>
<style>
input{
border-radius: 5px;
}
button{
border-radius: 5px;
font-size: 1.1em;
}
</style>
</head>
<body>
<h1>Web Programming</h1>
<p>๊ณต๋ถํ ์ฃผ์ ๋ฅผ ๊ธฐ๋กํด ๋ณด์ธ์</p>
<label for="input">์ฃผ์ </label>
<input id="input" type="text" autofocus>
<button class="button">์ถ๊ฐ</button>
<hr>
<ul class="itemList">
<li>์ถ๊ฐํ ๊ฒ์</li>
</ul>
<script>
const button = document.querySelector('.button');
const input = document.querySelector('#input');
const itemList = document.querySelector(".itemList");
document.addEventListener('DOMContentLoaded', function(){
button.addEventListener('click', function(e){
e.preventDefault();
var msg = input.value;
if(msg !== ""){
newRegister(msg);
input.value = "";
}
});
itemList.addEventListener('click', (e) => {
if(e.target.tagName === 'LI'){
if(confirm("์ญ์ ํ์๊ฒ ์ต๋๊น?")){
e.target.remove();
};
}
});
});
function newRegister(msg){
// ์์๋ฅผ ๋ง๋ค๊ณ
const newItem = document.createElement('li');
// ์์์ text ์ถ๊ฐ
const newText= document.createTextNode(msg);
// ์์์ ํ
์คํธ๋ฅผ ๋ฃ์ด์ฃผ๊ณ
newItem.appendChild(newText);
// ์ ์ฒด ๋ฆฌ์คํธ์ ์์ฑ๋ li๋ฅผ ๋ฃ์ด์ค๋ค
itemList.appendChild(newItem);
}
</script>
</body>
</html>