设计一个hmtl、css、JavaScript的点餐系统
时间: 2023-10-24 15:05:59 浏览: 108
基于java的点餐系统
好的,下面是一个简单的 HTML、CSS 和 JavaScript 点餐系统设计方案。系统包括菜单列表、购物车、结算功能。
HTML:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点餐系统</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>点餐系统</h1>
</header>
<main>
<section class="menu">
<h2>菜单</h2>
<ul>
<li>
<h3>鱼香肉丝</h3>
<p>15元/份</p>
<button class="add-to-cart">加入购物车</button>
</li>
<li>
<h3>宫保鸡丁</h3>
<p>18元/份</p>
<button class="add-to-cart">加入购物车</button>
</li>
<li>
<h3>酸菜鱼</h3>
<p>25元/份</p>
<button class="add-to-cart">加入购物车</button>
</li>
</ul>
</section>
<section class="cart">
<h2>购物车</h2>
<ul>
<li>
<h3>鱼香肉丝</h3>
<p>15元/份</p>
<button class="remove-from-cart">移除</button>
</li>
<li>
<h3>宫保鸡丁</h3>
<p>18元/份</p>
<button class="remove-from-cart">移除</button>
</li>
</ul>
<p class="total">总价:33元</p>
<button class="checkout">结算</button>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
```
CSS:
```css
body {
font-family: Arial, sans-serif;
}
header {
background-color: #333;
color: #fff;
padding: 20px;
}
h1 {
margin: 0;
}
.menu,
.cart {
width: 50%;
float: left;
padding: 20px;
}
ul {
list-style: none;
padding: 0;
}
li {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.add-to-cart,
.remove-from-cart {
border: none;
background-color: #333;
color: #fff;
padding: 5px 10px;
cursor: pointer;
}
.add-to-cart:hover,
.remove-from-cart:hover {
background-color: #555;
}
.total {
text-align: right;
margin-top: 20px;
}
.checkout {
border: none;
background-color: #333;
color: #fff;
padding: 10px;
margin-top: 10px;
cursor: pointer;
}
.checkout:hover {
background-color: #555;
}
```
JavaScript:
```javascript
const menuItems = document.querySelectorAll('.menu li');
const cartItems = document.querySelector('.cart ul');
const total = document.querySelector('.total');
const checkout = document.querySelector('.checkout');
let cartTotal = 0;
menuItems.forEach(item => {
const addToCartBtn = item.querySelector('.add-to-cart');
addToCartBtn.addEventListener('click', () => {
const itemName = item.querySelector('h3').textContent;
const itemPrice = Number(item.querySelector('p').textContent.match(/\d+/)[0]);
const cartItem = document.createElement('li');
cartItem.innerHTML = `<h3>${itemName}</h3><p>${itemPrice}元/份</p><button class="remove-from-cart">移除</button>`;
cartItems.appendChild(cartItem);
cartTotal += itemPrice;
total.textContent = `总价:${cartTotal}元`;
const removeFromCartBtn = cartItem.querySelector('.remove-from-cart');
removeFromCartBtn.addEventListener('click', () => {
cartItems.removeChild(cartItem);
cartTotal -= itemPrice;
total.textContent = `总价:${cartTotal}元`;
});
checkout.addEventListener('click', () => {
alert(`您需要支付${cartTotal}元`);
});
});
});
```
这个点餐系统的逻辑比较简单,当用户点击“加入购物车”按钮时,会将菜品名称、价格添加到购物车中。购物车中的列表中可以移除已选择的菜品,同时会实时更新总价。当用户点击“结算”按钮时,会弹出提示框告诉用户需要支付的总价。
阅读全文