首页 技术 正文
技术 2022年11月6日
0 收藏 642 点赞 288 浏览 1190 个字

文章地址 https://www.cnblogs.com/sandraryan/

写两个button和一个div,点击按钮分别改变背景色和前景色(字体颜色)。产生的是一个随机颜色。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
text-align: center;
}
#wrap {
width: 200px;height: 200px;border: 1px solid black;text-align: center;
margin: 10px auto;font-size: 30px;
}
button {
width: 150px;height: 40px;color: black;text-align: center;
margin: 10px;
}
</style>
</head>
<body>
<button id="btn1">change bgcolor</button><br>
<button id="btn2">change color</button>
<div id="wrap">
test text
</div> <script>
var btn1 = document.getElementById("btn1");
var btn2 = document.getElementById("btn2");
var wrap = document.getElementById("wrap");
// 获取元素 function getColor(){
// 封装随机生成颜色的函数作为返回值
return Math.round(Math.random()*255);
} function giveColor(){
var red = getColor();
var green = getColor();
var blue = getColor();
// 获取三个rgb的值
var color= "rgb(" + red + "," + green + "," + blue + ")";
// 吧rhb值拼接在一起给变量color
return color;
// 返回color
} btn1.onclick = function(){
wrap.style.backgroundColor = giveColor();
// btn1注册点击时间,改变wrap的背景色
} btn2.onclick = function(){
wrap.style.color = giveColor();
// btn1注册点击时间,改变wrap的字体颜色
} </script>
</body>
</html>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,701
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741