js实现点击切换背景颜色

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>换背景颜色</title>
    <style>
        #button1 {
            background-color: red;
        }
        
        #button2 {
            background-color: yellow;
        }
        
        #button3 {
            background-color: blue;
        }
    </style>
    <script>
        window.onload = function() {

            button1.onclick = function() {
                var test = body1.style.backgroundColor = "red";
                button1.innerHTML = test;
                /* alert("2"); */
            }
            button2.onclick = function() {
                var test = body1.style.backgroundColor = "yellow";
                button1.innerHTML = test;
            }
            button3.onclick = function() {
                var test = body1.style.backgroundColor = "blue";
                button1.innerHTML = test;
            }

        }
    </script>
</head>

<body id="body1">
    <input type="button" value="button1" id="button1">
    <input type="button" value="button2" id="button2">
    <input type="button" value="button3" id="button3">
</body>

</html>
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐