<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #box>div { height: 50px; width: 50px; border-radius: 50%; line-height: 50px; text-align: center; font-size: 20px; font-weight: bold; color: white; margin: 10px; float: left; } .red { background-color: red; } .blue { background-color: blue; } </style> </head> <body> <div id="box"> </div> <script> </script> </body> </html>根据此HTML写出双色球选号js部分
时间: 2023-12-10 14:38:59 浏览: 118
以下是双色球选号的js部分:
```javascript
let redBalls = []; //红球数组
let blueBall = 0; //蓝球
//生成红球随机数
function generateRedBall() {
let redBall = Math.floor(Math.random() * 33) + 1;
if (redBalls.includes(redBall)) { //如果随机数已经存在,重新生成
return generateRedBall();
} else {
redBalls.push(redBall);
}
}
//生成蓝球随机数
function generateBlueBall() {
blueBall = Math.floor(Math.random() * 16) + 1;
}
//生成双色球号码
function generateSSQ() {
redBalls = []; //清空红球数组
for (let i = 0; i < 6; i++) {
generateRedBall(); //生成6个红球
}
redBalls.sort((a, b) => a - b); //按升序排序
generateBlueBall(); //生成一个蓝球
displaySSQ(); //显示双色球号码
}
//显示双色球号码
function displaySSQ() {
let box = document.querySelector('#box');
box.innerHTML = ''; //清空盒子
redBalls.forEach(ball => { //显示红球
let div = document.createElement('div');
div.classList.add('red');
div.innerText = ball;
box.appendChild(div);
});
let div = document.createElement('div'); //显示蓝球
div.classList.add('blue');
div.innerText = blueBall;
box.appendChild(div);
}
//点击按钮生成双色球号码
let btn = document.createElement('button');
btn.innerText = '生成双色球号码';
btn.addEventListener('click', generateSSQ);
document.body.appendChild(btn);
```
阅读全文