批改状态:未批改
老师批语:
<!DOCTYPE html>
<html>
<head>
<title>js基础-math随机对象</title>
</head>
<body>
<button onclick="bg_color()">点击改变颜色 </button>
<script type="text/javascript">
//Math 对象的作用:执行普通的算数任务;
//Math 对象提供多种算数值类型和函数,无需再使用这个对象之前对她进行定义;
//round()方法可以把一个数字四舍五入为最接近的整数;
//语法:Math.round(x);x必须为数字;
// round 四舍五入
// random 0-1之间的随机数;
// floor 向下取整;
// var a=Math.round(3.5)
// document.write(a+'<br>');
// // random()方法可返回介于0<=x<1之间的随机数;
// document.write(Math.random()+'<br>');
// //floor()方法返回小于等于x的最大整数;(是整数值不变);
// var b=Math.floor(3.9);
// document.write(b+'<br>')
// // 案例:取2介于1到10之间的随机数;+1是为了防止得到的答案出现1;
// var c=Math.floor((Math.random()*10+1))
// document.write(c+'<br>')
//随机改变背景颜色方法一:
// function bg_color(){
// //拼接16进制的颜色
// var bg='#';
// // toString()强制转成字符串类型;字符串加数组等于字符串;
// var a=Math.floor(Math.random()*10).toString()+Math.floor(Math.random()*10)
// var b=Math.floor(Math.random()*10).toString()+Math.floor(Math.random()*10)
// var c=Math.floor(Math.random()*10).toString()+Math.floor(Math.random()*10)
// bg+=a+b+c
// //点击之后;会发生什么?
// // console.log(bg)
// document.getElementsByTagName('body')[0].style.background=bg;
// }
//随机改变背景颜色方法二:
function bg_color(){
//思路: 产生一个随机小数,乘以1000000,
// 然后四舍五入(向下取整也行);得到一个6位数的整数,
// 前面拼接一个#,这样就可以得到一个16进制的..;
// 产生一个随机小数,乘以1000000
var a=Math.random()*1000000;
// document.write(a);
// 然后四舍五入(向下取整也行);
var b=Math.round(a);
// document.write(b);
// 拼接16进制的颜色
//字符串加数字等于字符串;
var c='#'+b;
// document.write(c)
document.getElementsByTagName('body')[0].style.background=c;
}
</script>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号