λͺ©μ°¨
μΉνμ΄μ§μ μ΄λ ν μ΄λ²€νΈκ° λ°μνμ λ
κ·Έμ λν΄ μ¬μ©μκ° μνΈμμ©μ ν μ μκ² νλ κ²μ λν΄μ μμ보μ.
μμ μλ νκ·Έμ μ§μ μμ±μΌλ‘ μλ²€νΈλ₯Ό μΆκ°νλ λ°©μλ μμμ§λ§,
μ΅κ·Όμλ 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 |
λκΈ