[JavaScript] 버튼 눌러서 배경 색상 랜덤으로 바꾸기

박현아·2025년 5월 2일
0

JavaScript

목록 보기
4/4
post-thumbnail
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function changeColor() {
            let arrColor = ["red", "orange", "yellow", "green", "blue"];
            let arrNum = Math.floor(Math.random()*arrColor.length); // 0~5
            // console.log(arrNum);
            let tag = document.getElementById("theBody");
            tag.style.backgroundColor = arrColor[arrNum];
        }
    </script>
</head>
<body id="theBody">
    <button onclick="changeColor()">배경 색상 바꾸기</button>
</body>
</html>

0개의 댓글