Linux 拨号vps windows公众号手机端

html点击按钮改变背景颜色怎么实现

lewis 6年前 (2019-03-20) 阅读数 13 #程序编程
文章标签 html

html点击按钮改变背景颜色的实现方法:1.创建一个html文件;2.在html文件中添加html代码架构;3.在body标签里面使用input标签实现一个按钮;4.在html标签里面添加script标签并设置点击按钮改变颜色的功能;5.通过浏览器方式查看设置效果。

html点击按钮改变背景颜色的实现方法:

1.首先创建一个html文件。

2.在html文件中添加html代码架构。

<!DOCTYPEhtml>

<html>

<head>

<title>改变颜色</title>

</head>

<body>

</body>

</html>

3.然后在html代码架构中的body标签里面使用input标签实现一个按钮。

<inputtype="button"value="点击切换颜色"id="btn">

4.在html架构中的html标签里面添加script标签并设置点击按钮改变颜色的功能。

<script>

varbtn=document.getElementById("btn");

varbody=document.body;

letarray=["red","blue","yellow","white","black","green","orange","#FFF"];

letindex=0;

btn.onclick=function(){

body.style.backgroundColor=array[index];

index=(index+1)%array.length;

}

</script>

5.最后可通过浏览器方式阅读html文件查看设置效果。

完整示例代码如下:

<!DOCTYPEhtml>

<html>

<head>

<metacharset="UTF-8">

<title>改变颜色</title>

</head>

<body>

<inputtype="button"value="点击切换颜色"id="btn">

</body>

<script>

varbtn=document.getElementById("btn");

varbody=document.body;

letarray=["red","blue","yellow","white","black","green","orange","#FFF"];

letindex=0;

btn.onclick=function(){

body.style.backgroundColor=array[index];

index=(index+1)%array.length;

}

</script>

</html>

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门