超简单的水果订单网页

对水果的数量,单价进行添加,删除,添加水果时若字段为空会有提示,简单的小网页,封面图就是运行图

HTML部分:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>水果订单</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <div class="container">
    <h1>水果订单</h1>
    <div id="fruits">
      <!-- 水果项目将动态添加到这里 -->
    </div>
    <div id="total">总价:$<span id="totalPrice">0</span></div>
    <div id="controls">
      <input type="text" id="fruitName" placeholder="水果名称">
      <input type="number" id="fruitQuantity" placeholder="数量">
      <input type="number" id="fruitPrice" placeholder="单价">
      <button onclick="addFruit()">添加水果</button>
    </div>
  </div>
  <script src="script.js"></script>
</body>
</html>

CSS部分(styles.css):

body {
  font-family: Arial, sans-serif;
  background-color: #ecf0f1; /* 主要色调绿色和白色 */
}

.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
}

#fruits {
  margin-bottom: 20px;
}

.fruit-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
}

#total {
  text-align: right;
  margin-bottom: 10px;
}

#controls {
  display: flex;
}

input[type="text"],
input[type="number"] {
  flex: 1;
  margin-right: 10px;
}

button {
  background-color: #2ecc71; /* 主要色调绿色 */
  color: #fff;
  border: none;
  padding: 8px 15px;
  border-radius: 5px;
  cursor: pointer;
}

button:hover {
  background-color: #27ae60; /* 深绿色 */
}

JavaScript部分(script.js):

let fruits = [];

function renderFruits() {
  const fruitsDiv = document.getElementById('fruits');
  fruitsDiv.innerHTML = '';
  
  fruits.forEach(fruit => {
    const fruitDiv = document.createElement('div');
    fruitDiv.classList.add('fruit-item');
    fruitDiv.innerHTML = `
      <span>${fruit.name}:${fruit.quantity} x $${fruit.price}</span>
      <span>$${fruit.quantity * fruit.price}</span>
      <button onclick="removeFruit('${fruit.name}')">移除</button>
    `;
    fruitsDiv.appendChild(fruitDiv);
  });
  
  updateTotal();
}

function updateTotal() {
  const totalPriceSpan = document.getElementById('totalPrice');
  let total = 0;
  fruits.forEach(fruit => {
    total += fruit.quantity * fruit.price;
  });
  totalPriceSpan.textContent = total.toFixed(2);
}

function addFruit() {
  const nameInput = document.getElementById('fruitName');
  const quantityInput = document.getElementById('fruitQuantity');
  const priceInput = document.getElementById('fruitPrice');
  
  const name = nameInput.value;
  const quantity = parseInt(quantityInput.value);
  const price = parseFloat(priceInput.value);
  
  if (name && quantity && price) {
    const existingFruitIndex = fruits.findIndex(fruit => fruit.name === name);
    if (existingFruitIndex !== -1) {
      fruits[existingFruitIndex].quantity += quantity;
    } else {
      fruits.push({ name, quantity, price });
    }
    
    renderFruits();
    
    nameInput.value = '';
    quantityInput.value = '';
    priceInput.value = '';
  }
}

function removeFruit(name) {
  fruits = fruits.filter(fruit => fruit.name !== name);
  renderFruits();
}

window.onload = function() {
  renderFruits();
};
  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值