Use js to realize clicking the button to modify the color of the div

Content description: Modify the color of the div when the button button is clicked

code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #div1 {
            width: 100px;
            height: 100px;
            border: 1px solid black;
        }

        .box {
            background-color: red;
        }
    </style>
    <script>
        function f() {
            var Div1 = document.getElementById("div1");
            Div1.className = "box";
        }
    </script>
</head>
<body>
<input type="button" onclick="f()" value="修改div颜色">
<div id="div1"></div>
</body>
</html>

Renderings:

Initial image:

click button:

 

 

Guess you like

Origin blog.csdn.net/psjasf1314/article/details/124104519