用html+css+js写一个购票网页
时间: 2024-11-04 18:20:01 浏览: 2
创建一个简单的购票网页,我们可以使用HTML构建页面结构,CSS负责样式设计,JavaScript用于交互功能。以下是一个基本的例子:
```html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>在线购票系统</title>
<link rel="stylesheet" href="styles.css"> <!-- 引入CSS文件 -->
</head>
<body>
<header>
<h1>在线购票</h1>
</header>
<main>
<form id="bookingForm">
<label for="ticketType">票种:</label>
<select id="ticketType" name="ticketType">
<option value="成人">成人票</option>
<option value="儿童">儿童票</option>
</座位数量>
<input type="number" id="seatCount" name="seatCount" min="1" max="5">
<button type="submit">立即购买</button>
</form>
<div id="confirmationMessage" class="hidden"></div>
</main>
<script src="scripts.js"> <!-- 引入JavaScript文件 -->
</script>
</body>
</html>
```
接下来是`styles.css`中的样式部分:
```css
/* styles.css */
body {
font-family: Arial, sans-serif;
}
header {
background-color: #4CAF50;
color: white;
padding: 10px;
}
form {
margin-top: 20px;
}
button {
background-color: dodgerblue;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
}
.hidden {
display: none;
}
```
最后,`scripts.js`中的JavaScript部分可以处理表单提交并显示确认信息:
```javascript
// scripts.js
document.getElementById('bookingForm').addEventListener('submit', function(e) {
e.preventDefault(); // 阻止默认提交
const ticketType = document.getElementById('ticketType').value;
const seatCount = parseInt(document.getElementById('seatCount').value);
if (isNaN(seatCount) || seatCount < 1) {
alert("请输入有效的座位数量!");
return;
}
const confirmationMessage = `已为您预订${seatCount}张${ticketType}票。`;
document.getElementById('confirmationMessage').innerText = confirmationMessage;
document.getElementById('confirmationMessage').classList.remove('hidden');
});
```
阅读全文