⽤JavaScript+HTML实现双⾊球随机摇号效果⽤JavaScript+HTML实现双⾊球随机摇号效果
⾸先要知道双⾊球的规则是什么
红球号码范围为01~33,蓝球号码范围为01~16
双⾊球每期从33个红球中开出6个号码,从16个蓝球中开出1个号码作为中奖号码
我的高中生活作文双⾊球玩法即是竞猜开奖号码的6个红球号码和1个蓝球号码,顺序不限
知道了规则以后,我们就可以⽤代码实现了,废话不到说,直接上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>双⾊球</title>
<style>
span {
/*display: none;*/
margin: 5px;
font-size: 30px;
width: 70px;
height: 70px;
background-color: red;
color: white;
text-align: center;
郑嘉颖刘心悠
line-height: 70px;
display: inline-block;
border-radius: 50%;
}
立春节气的风俗活动.blue {
background-color: blue;
}
button {
width: 75px;
height: 40px;
font-size: 20px;
border: none;
color: white;
background-color: gray;
cursor: pointer;
}
</style>
</head>
<body>
<p>
<span class="red"></span>
<span class="red"></span>
<span class="red"></span>
<span class="red"></span>
4.14是什么情人节?<span class="red"></span>
<span class="red"></span>
<span class="blue"></span>
</p>
<button>摇号</button>
</body>
</html>
<script>
// 红⾊球号码从1--33中选择;蓝⾊球号码从1--16中选择
let spanRed = document.querySelectorAll('.red');
let spanBlue = document.querySelector('.blue');
let btn = document.querySelector('button')
网游排行榜前十/**
* 返回⼀个指定数值范围(start~end)内的、元素互不重复的、且长度为len随机数组
* @param start
* @param end
* @param len
* @returns {any[]}
*/
function maker(start, end, len) {
let set = new Set(new Array(len));
set.clear();
for (; ;) {
let ok = und(Math.random() * (end - start)) + start; if (!set.has(ok)) {
set.add(ok);
}
if (set.size === len) {
break;
}
}
//集合转化为数组
return Array.from(set);
医保怎么交费}
function randomNumber() {
let red = maker(1, 33, spanRed.length);
for (let i = 0; i < spanRed.length; i++) {
spanRed[i].innerHTML = red[i];
}
let blue = maker(1, 16, 1);
spanBlue.innerHTML = blue[0];
}
randomNumber();
btn.addEventListener('click', randomNumber);
</script>
运⾏后如下:
点击摇号,产⽣⼀组随机数
发布评论