web/코딩

웹 (html,css,javascript) 예제 - 무작위로 바뀌는 배경색

aphyrince 2022. 1. 8. 02:30
반응형
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>버튼 누르면 색 바뀜</title>
    <script>
        window.onload= function(){
        var button1 = document.getElementById("button1");
        button1.onclick = changeBack;

        function changeBack(){
            document.body.style.backgroundColor=getRandomRgb();
            console.log(getRandomRgb());
        }

        function getRandomRgb(){
            var red = parseInt(Math.random()*255);
            var green = parseInt(Math.random()*255);
            var blue = parseInt(Math.random()*255);
            return "rgb("+red+","+green+","+blue+")";
        }
    }
    </script>
</head>
<body>
    <input id="button1" type="button" value="change!!">
</body>
</html>
반응형