์นํ์ด์ง์ ์ด๋ ํ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์ ๋
๊ทธ์ ๋ํด ์ฌ์ฉ์๊ฐ ์ํธ์์ฉ์ ํ ์ ์๊ฒ ํ๋ ๊ฒ์ ๋ํด์ ์์๋ณด์.
์์ ์๋ ํ๊ทธ์ ์ง์ ์์ฑ์ผ๋ก ์๋ฒคํธ๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ์๋ ์์์ง๋ง,
์ต๊ทผ์๋ JS๋ฅผ HTML๊ณผ ๋ถ๋ฆฌํ๊ธฐ์ํด์
addEventListener()๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ์์ ์ ํธํ๋ค.
์ด๋ฒคํธ ๋ฑ๋ก
๋์ ์์.addEvenListener(์ด๋ฒคํธ๋ช
, ๋ฆฌ์ค๋ํจ์(์ฝ๋ฐฑํจ์))
์ด๋ฒคํธ๋ช ์
click, intput, focus, mousehover ๋ฑ์ ์ด์ผ๊ธฐํ๋ค.
๋ฆฌ์ค๋ํจ์๋ ๋ง๊ทธ๋๋ก 'ํจ์'๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
๋ฐ๋ผ์ ์ธ์ ์๋ฆฌ์ ํจ์๋ฅผ ์ ์ํด์ฃผ๊ฑฐ๋ ์ ์ํ ํจ์์ ์ด๋ฆ์ ๋ฃ์ด์ค๋ค.
(ํจ์์ ์ด๋ฆ์ด ์๋ "ํจ์๋ช ()"์ ํํ๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋ฐํ๊ฐ์ด ๋ค์ด๊ฐ๋ฏ๋ก ์ ์์๋X)
์ด๋ฒคํธ ์ญ์
๋์ ์์.removeEvenListener(์ด๋ฒคํธ๋ช
, ์ด๋ฒคํธ ๋ฐ์์ ์คํ ํจ์)
๋ฑ๋กํ ๋ ์ ์ฉํ ์ธ์๋ฅผ ๊ทธ๋๋ก ์จ์ฃผ์ด์ผ ํ๋ค.
์์ 1
<!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>
.box1 {
width: 100px;
height: 100px;
border-radius: 5px;
line-height: 100px;
background: blueviolet;
text-align: center;
}
</style>
</head>
<body>
<div class="box1">Hello!</div>
<script>
const box1 = document.querySelector(".box1") //์ ํ์๋ฅผ ์ด์ฉํด ๋ณ์์ ๋ด๊ธฐ
box1.addEventListener("click", () => { //ํด๋น ๋ธ๋ก์ ํด๋ฆญํ์ ๋
alert("Hello!"); //์๋ฆผ์ฐฝ ๋์ฐ๊ธฐ
});
</script>
</body>
</html>

์์ 2
addEventListener ํ์ฉ
๋ ธ๋์ ๋ฒํผ์ ์์ฑ, ์ฃผํฉ์ ๋ฒํผ์ ์ญ์ ๋ฅผ ํ๋ ์ฝ๋์ด๋ค.
<!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>
.container {
display: flex;
flex-direction: row;
}
.button1 {
width: 50px;
height: 20px;
background: gold;
}
.button2 {
width: 50px;
height: 20px;
background: orangered;
}
.box1 {
width: 100px;
height: 100px;
border-radius: 5px;
line-height: 100px;
background: blueviolet;
text-align: center;
margin: 2px;
}
</style>
</head>
<body>
<div class="container">
<div class="button1"></div>
<div class="button2"></div>
</div>
<script>
const button1 = document.querySelector(".button1")
const button2 = document.querySelector(".button2")
let newBox = document.createElement("div")
button1.addEventListener("click", () => {
newBox.setAttribute("class", "box1")
document.body.appendChild(newBox)
});
button2.addEventListener("click", () => {
document.body.removeChild(newBox)
});
</script>
</body>
</html>

์์ 3
removeEventListener ํ์ฉ
๋ณด๋ผ์ ๋ธ๋ก์ ๋๋ ์ ๋
์ญ์ ๋ฒํผ์ ์ด๋ฒคํธ๋ฅผ ์ญ์ ํ๋ ์ฝ๋์ด๋ค.
<!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>
div:hover {
cursor: pointer;
}
.container {
display: flex;
flex-direction: row;
}
.button1 {
width: 50px;
height: 20px;
background: gold;
}
.button2 {
width: 50px;
height: 20px;
background: orangered;
}
.box1 {
width: 100px;
height: 100px;
border-radius: 5px;
line-height: 100px;
background: blueviolet;
text-align: center;
margin: 2px;
}
</style>
</head>
<body>
<div class="container">
<div class="button1"></div>
<div class="button2"></div>
</div>
<script>
const createBox = () => {
newBox.setAttribute("class", "box1")
document.body.appendChild(newBox)
}
const removeBox = () => {
document.body.removeChild(newBox)
}
const button1 = document.querySelector(".button1")
const button2 = document.querySelector(".button2")
let newBox = document.createElement("div")
button1.addEventListener("click", createBox);
button2.addEventListener("click", removeBox);
newBox.addEventListener("click", () =>{
newBox.innerText = "Fixed!"
button2.removeEventListener("click", removeBox);
})
</script>
</body>
</html>

์์4
์ฌ์ฉ์๋ก๋ถํฐ์ ์ ๋ ฅ๊ฐ ํ์ฉ
<input type="text" name="" id="word-input">
<button>์
๋ ฅ</button>
<script>
document.querySelector('input').addEventListener('input', (event) => {
console.log(event.target.value);
});
document.querySelector('button').addEventListener('click', () => {
console.log("๋ฒํผ ํด๋ฆญ!");
});
</script>

event๋ฅผ ๋ฆฌ์ค๋ํจ์์ ์ธ์๋ก ์ฝ์ ํ๊ณ
event.target.value๋ฅผ ์ด์ฉํ์ฌ ์ ๋ ฅ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
input ์ด๋ฒคํธ๋ ์ ๋ ฅ์ฐฝ์ ๋ณํ๊ฐ ์์ ๋๋ง๋ค ์ผ์ด๋๋ฏ๋ก
๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ์ผ์ด๋ ์์ ์ ์ง์ ์ํ๋ฅผ ์ด์ฉํ๋ฉด
์ ์ ๊ฐ ์๋ํ ๊ฐ์ ํ์ฉํ ์ ์๋ค.
โป.vlaue / .textContent
์์์ ์ข ๋ฅ์ ๋ฐ๋ผ์ ๋ด๋ถ์ ๊ฐ์ ๊ฐ์ ธ์ฌ ๋ .value ์ .textContent๋ก ๋ค๋ฅธ ๊ฒฝ์ฐ๊ฐ ์๋ค.
์ด๋ฅผ ์ผ๋ฐํ์ํค๋ฉด '์ ๋ ฅ์ ํ๋ ๊ฒฝ์ฐ'์๋ value์ด๋ค.
( .value : input, select, textarea ๋ฑ)
( .textContent : button, div, span ๋ฑ)
'๐ | WEB DEV > Vanilla JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JS_๊ธฐ๋ณธ ๋ฌธ๋ฒ (11)_ํ ํ๋ฆฟ ๋ฌธ์์ด (0) | 2022.11.23 |
---|---|
JS_๋ฌธ๋ฒ (10)_๋ํ์ฐฝ (0) | 2022.07.26 |
JS_๋ฌธ๋ฒ (8)_DOM CRUD ํํค์น๊ธฐ (0) | 2022.02.24 |
JS_๋ฌธ๋ฒ (7)_DOM / BOM (0) | 2022.02.23 |
JS_๋ฌธ๋ฒ (6)_promise ๋ฉ์๋(catch, finally, all, race ๋ฑ ) (0) | 2022.02.20 |
๋๊ธ