javascript click to change button color

PHPz
Release: 2023-05-16 13:12:38
Original
3348 people have browsed it

JavaScript is one of the essential languages ​​​​in modern web development, which adds interactivity and dynamics to web pages. Among them, clicking a button to change the color is a small function that can be implemented after getting started with JavaScript.

In this article, we will explore how to use JavaScript to interactively modify the color of the buttons on the web page, thereby making the web page more dynamic and attractive.

1. HTML basics

First, we need to create the HTML code containing the button, as shown below:

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript按钮颜色修改</title>
</head>
<body>

<button id="btn" onclick="changeColor()">点击我改变颜色</button>

</body>
</html>
Copy after login

Here, we create a button containing a The button's web page and adds an ID and a click event to the button. Next, we need to use JavaScript to write the action of changing the button color.

2. JavaScript implementation

Next, we need to introduce JavaScript code into the HTML code to achieve the dynamic effect of button color. In the following JavaScript code, we create a function changeColor() to change the background color of the button.

function changeColor(){
    //获取按钮节点
    var btn = document.getElementById("btn");
    
    //生成随机颜色值
    var randomColor = Math.floor(Math.random()*16777215).toString(16);
    
    //改变按钮颜色
    btn.style.backgroundColor = "#" + randomColor;
}
Copy after login

Here, we first get the button node. Then, use the Math.random() function to generate a random hexadecimal color value and assign this value to the button's background color property. So when the button is clicked, the button's background color will change randomly.

3. Complete code

Finally, we combine the HTML code and JavaScript code to achieve a complete button color interaction effect.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript按钮颜色修改</title>
    <script>
        function changeColor(){
            //获取按钮节点
            var btn = document.getElementById("btn");
            
            //生成随机颜色值
            var randomColor = Math.floor(Math.random()*16777215).toString(16);
            
            //改变按钮颜色
            btn.style.backgroundColor = "#" + randomColor;
        }
    </script>
</head>
<body>

<button id="btn" onclick="changeColor()">点击我改变颜色</button>

</body>
</html>
Copy after login

At this point, we have successfully implemented the interactive effect of changing the color of web page buttons through JavaScript. Note that in actual development, we need to use JavaScript reasonably to avoid excessive client-side computing and performance problems. Therefore, we need to comprehensively consider it in specific projects.

In short, JavaScript provides a wealth of functions and APIs, allowing us to add various dynamic effects and interactive functions to web pages. When using JavaScript, we need to fully understand its basic syntax and principles in order to carry out reasonable development.

The above is the detailed content of javascript click to change button color. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template