내가 작성한 jQuery
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<style type="text/css">
.color{
height: 200px;
width: 100px;
background-color: blue;
}
</style>
</head>
<body>
<script type="text/javascript">
/*
버튼을 누르면
화면에 빨간상자가 파란색
한번더 누르면
파란 >> 빨강
*/
$(document).ready(function() {
$('.color-change').click(function() {
if($('.color').css('background-color') == 'rgb(255, 0, 0)'){
$('.color-box > div').css('background-color','blue');
console.log($('.color').css('background-color'));
}
else{
$('.color-box > div').css('background-color','red');
console.log($('.color').css('background-color'));
}
});
});
</script>
<div class="btn-box">
<button class="color-change">색변경</button>
</div>
<div class="color-box">
<div class="color"></div>
</div>
</body>
</html>
강사님이 작성하신 jQuery 답안
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<style type="text/css">
#box {
width: 100px;
height: 100px;
margin: 5px;
}
.off {
background-color: red;
}
.on {
background-color: blue;
}
</style>
</head>
<body>
<script>
$(document).ready(function() {
$('#btn').click(function() {
$('#box').toggleClass('off on');
});
});
</script>
</head>
<body>
<button id="btn">액션</button>
<div id="box" class="off"></div>
</body>
</html>
728x90
'국비 내용 정리 > 문제' 카테고리의 다른 글
[Spring] AOP @(어노테이션) 실습 (0) | 2024.10.16 |
---|---|
국비 28일차 실습 문제 (0) | 2024.08.11 |
JSP 이해하기 문제(ID 비교) (0) | 2024.08.06 |