<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>초에 따라 배경색 바꾸기</title>
<script>
//버튼을 클릭할 때 변화
function change(){
const date = new Date();
let hour = date.getHours();
let minute = date.getMinutes();
let second = date.getSeconds();
//초가 짝수일때 홀수일때 배경색을 다르게 준다.
second % 2 === 0 ? (document.body.style.backgroundColor = "red") : (document.body.style.backgroundColor = "lightblue");
let spanTime = document.querySelector("#spanTime");
spanTime.innerHTML = `현재시간보이기 : ${hour}시 ${minute}분 ${second}초`;
}
</script>
</head>
<body>
<h1>배경색 바꾸기 </h1>
<hr>
<fieldset>
<legend>초에 따라 배경색 바꾸기</legend>
<span id="spanTime">현재시간보이기 : 14시 9분 56초</span>
<button type="button" onclick="change()">배경색바꾸기, 시간보이기 이벤트</button>
</fieldset>
</body>
</html>