<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>문제1</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
.wrap{
width:500px;
margin:100px auto;
display:flex;
justify-content:space-between;
}
.box{
width:150px;
height:150px;
background-color:lightgray;
border-radius:10px;
cursor:pointer;
transition:0.3s;
}
.active{
background-color:tomato;
}
</style>
</head>
<body>
<div class="wrap">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<script>
const boxAll = document.querySelectorAll('.box');
for(let i = 0; i < boxAll.length; i++){
boxAll[i].addEventListener('click', () => {
boxAll[i].classList.toggle('active');
});
}
</script>
</body>
</html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>문제1</title>
<style>
*{
margin:0;
padding:0;
box-sizing:border-box;
}
.wrap{
width:500px;
margin:100px auto;
display:flex;
justify-content:space-between;
}
.box{
width:150px;
height:150px;
background-color:lightgray;
border-radius:10px;
cursor:pointer;
transition:0.3s;
}
.active{
background-color:tomato;
}
</style>
</head>
<body>
<div class="wrap">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<script>
const boxAll = document.querySelectorAll('.box');
for(let i = 0; i < boxAll.length; i++){
boxAll[i].addEventListener('click', () => {
boxAll[i].classList.toggle('active');
});
}
</script>
</body>
</html>
#문제 1 : 박스 클릭 시 색 변경