web前端双色球代码
时间: 2023-07-16 19:15:54 浏览: 109
以下是一个简单的双色球选号器的前端代码示例(使用HTML、CSS和JavaScript):
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<title>双色球选号器</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>双色球选号器</h1>
<div id="container">
<p>请从下面的红球中选择6个号码:</p>
<div id="red">
<button class="red-ball">01</button>
<button class="red-ball">02</button>
<button class="red-ball">03</button>
<button class="red-ball">04</button>
<button class="red-ball">05</button>
<button class="red-ball">06</button>
<button class="red-ball">07</button>
<button class="red-ball">08</button>
<button class="red-ball">09</button>
<button class="red-ball">10</button>
<button class="red-ball">11</button>
<button class="red-ball">12</button>
<button class="red-ball">13</button>
<button class="red-ball">14</button>
<button class="red-ball">15</button>
<button class="red-ball">16</button>
<button class="red-ball">17</button>
<button class="red-ball">18</button>
<button class="red-ball">19</button>
<button class="red-ball">20</button>
<button class="red-ball">21</button>
<button class="red-ball">22</button>
<button class="red-ball">23</button>
<button class="red-ball">24</button>
<button class="red-ball">25</button>
<button class="red-ball">26</button>
<button class="red-ball">27</button>
<button class="red-ball">28</button>
<button class="red-ball">29</button>
<button class="red-ball">30</button>
<button class="red-ball">31</button>
<button class="red-ball">32</button>
<button class="red-ball">33</button>
</div>
<p>请从下面的蓝球中选择1个号码:</p>
<div id="blue">
<button class="blue-ball">01</button>
<button class="blue-ball">02</button>
<button class="blue-ball">03</button>
<button class="blue-ball">04</button>
<button class="blue-ball">05</button>
<button class="blue-ball">06</button>
<button class="blue-ball">07</button>
<button class="blue-ball">08</button>
<button class="blue-ball">09</button>
<button class="blue-ball">10</button>
<button class="blue-ball">11</button>
<button class="blue-ball">12</button>
<button class="blue-ball">13</button>
<button class="blue-ball">14</button>
<button class="blue-ball">15</button>
</div>
<button id="generate">生成选号</button>
<div id="result">
<p>您选择的号码是:</p>
<p id="red-result"></p>
<p id="blue-result"></p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS:
```css
h1 {
text-align: center;
}
#container {
margin: 50px auto;
width: 500px;
text-align: center;
}
#red, #blue {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: center;
margin-bottom: 20px;
}
.red-ball, .blue-ball {
border: none;
background-color: #f44336;
color: #fff;
font-size: 16px;
font-weight: bold;
width: 40px;
height: 40px;
border-radius: 50%;
margin: 5px;
cursor: pointer;
}
.blue-ball {
background-color: #2196F3;
}
#generate {
background-color: #4CAF50;
color: #fff;
font-size: 16px;
font-weight: bold;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
#result {
margin-top: 30px;
display: none;
}
```
JavaScript:
```javascript
// 获取DOM元素
var redBalls = document.querySelectorAll('.red-ball');
var blueBalls = document.querySelectorAll('.blue-ball');
var generateBtn = document.querySelector('#generate');
var redResult = document.querySelector('#red-result');
var blueResult = document.querySelector('#blue-result');
var resultDiv = document.querySelector('#result');
// 存储选中的号码
var redSelected = [];
var blueSelected = '';
// 添加红球点击事件
for (var i = 0; i < redBalls.length; i++) {
redBalls[i].addEventListener('click', function() {
if (this.classList.contains('selected')) {
this.classList.remove('selected');
redSelected.splice(redSelected.indexOf(this.innerHTML), 1);
} else {
if (redSelected.length === 6) {
alert('最多只能选择6个红球!');
return;
}
this.classList.add('selected');
redSelected.push(this.innerHTML);
}
console.log(redSelected);
});
}
// 添加蓝球点击事件
for (var i = 0; i < blueBalls.length; i++) {
blueBalls[i].addEventListener('click', function() {
if (this.classList.contains('selected')) {
this.classList.remove('selected');
blueSelected = '';
} else {
this.classList.add('selected');
blueSelected = this.innerHTML;
}
console.log(blueSelected);
});
}
// 添加生成选号按钮点击事件
generateBtn.addEventListener('click', function() {
if (redSelected.length !== 6 || blueSelected === '') {
alert('请选择6个红球和1个蓝球!');
return;
}
redResult.innerHTML = '红球:' + redSelected.join(', ');
blueResult.innerHTML = '蓝球:' + blueSelected;
resultDiv.style.display = 'block';
});
```
这段代码创建了一个简单的双色球选号器,用户可以从红球中选择6个号码,从蓝球中选择1个号码,然后点击“生成选号”按钮,程序会显示用户选择的号码。
阅读全文