html如何做一个抽奖
要创建一个抽奖活动,你可以使用HTML、CSS和JavaScript,以下是一个简单的示例:

1、创建一个HTML文件,添加以下内容:
抽奖活动
欢迎参加抽奖活动!
请在下面的表格中填写您的姓名和联系方式,然后点击“提交”按钮。
| 姓名 | 联系方式 |
|---|
2、接下来,添加CSS样式,在标签内添加以下内容:
body {
fontfamily: Arial, sansserif;
}
h1 {
textalign: center;
}
form {
display: flex;
flexdirection: column;
alignitems: center;
marginbottom: 20px;
}
input {
margin: 5px;
}
button {
margintop: 10px;
}
table {
width: 100%;
bordercollapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
textalign: left;
}
th {
backgroundcolor: #f2f2f2;
}
3、添加JavaScript代码,在标签内添加以下内容:
document.getElementById('participantForm').addEventListener('submit', function (event) {
event.preventDefault(); // 阻止表单默认提交行为
const name = document.getElementById('name').value;
const contact = document.getElementById('contact').value;
const table = document.getElementById('participantsTable').getElementsByTagName('tbody')[0];
const newRow = table.insertRow(table.rows.length);
const nameCell = newRow.insertCell(0);
const contactCell = newRow.insertCell(1);
nameCell.innerHTML = name;
contactCell.innerHTML = contact;
document.getElementById('name').value = '';
document.getElementById('contact').value = '';
document.getElementById('drawButton').disabled = false;
});
document.getElementById('drawButton').addEventListener('click', function () {
const participants = Array.from(document.getElementById('participantsTable').getElementsByTagName('tbody')[0].rows);
const randomIndex = Math.floor(Math.random() * participants.length);
const winner = participants[randomIndex];
document.getElementById('result').innerHTML = '恭喜 ' + winner.cells[0].innerHTML + ' 获得了奖品!';
document.getElementById('drawButton').disabled = true;
});
现在,你可以在浏览器中打开这个HTML文件,填写参与者信息并点击“开始抽奖”按钮,抽中的获奖者将在页面上显示。
分享题目:html如何做一个抽奖
地址分享:http://www.jxjierui.cn/article/cdiigje.html


咨询
建站咨询
