프런트엔드-자바스크립트

자바스크립트 핵심

강사: 정프런트 · 작성자: 이종춘 · 작성일: 2026-06-11 21:13:01 · 조회수: 18

문제 3 : 좋아요 버튼 만들기
목록으로
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>문제3</title>

<style>

*{
margin:0;
padding:0;
box-sizing:border-box;
}

.wrap{
width:400px;
margin:100px auto;
padding:30px;
text-align:center;
border:1px solid #ddd;
border-radius:15px;
}

.heart{
font-size:60px;
color:#999;
cursor:pointer;
transition:0.3s;
}

.heart.active{
color:red;
}

.like{
margin-top:20px;
font-size:24px;
}

</style>
</head>
<body>

<div class="wrap">

<div class="heart">♥</div>

<p class="like">좋아요 <span>0</span>개</p>

</div>

<script>

const heartEl = document.querySelector('.heart');
const countEl = document.querySelector('.like span');

let count = 0;
let isLiked = false;

heartEl.addEventListener('click', () => {

if(isLiked === false){

heartEl.classList.add('active');
count++;
isLiked = true;

}else{

heartEl.classList.remove('active');
count--;
isLiked = false;

}

countEl.innerText = count;

});

</script>

</body>
</html>
#문제 3 : 좋아요 버튼 만들기

첨부 파일

0개
첨부된 파일이 없습니다.

댓글 / 답글

0개
댓글을 작성하려면 로그인하세요.
아직 댓글이 없습니다.