js实现简单购物车

先上样式
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
   margin:0;padding:0;}
        table{
   
            width:1000px;
            border:collapse;
            margin:100px auto 0;
        }
        table td{
   
            border:1px solid #930;
        }
        table tr td{
   
            width:250;
            height:100px;
            text-align:center;
        }
        table tr.title td{
   
            height:50px;
            background:#c36;
            color:#fff;
            font-weight:bold;
            font-size:14px;
        }
        table tr td img{
   
            width:100px;
        }
        table tr td.number span{
   
            display:inline-block;
            font-size:14px;
            text-align:center;
        }
        table tr td.number span.down,
        table tr td.number span.add{
   
            width:30px;
            height:30px;
            background:pink;
            font-weight:bold;
            font-size:14px;
            line-height:30px;
            cursor:pointer;
        }
        table tr td.number span.num{
   
            width:50px;
            height:30px;
            line-height:30px;
            border-top:1px solid #ddd;
            border-bottom:
  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的JavaScript程序实现简易购物的示例: ```javascript // 初始化商品列表 const products = [ { id: 1, name: '商品1', price: 100 }, { id: 2, name: '商品2', price: 200 }, { id: 3, name: '商品3', price: 300 }, { id: 4, name: '商品4', price: 400 }, ] // 初始化购物 let cart = [] // 添加商品到购物 function addToCart(id) { const product = products.find(p => p.id === parseInt(id)) if (product) { cart.push(product) console.log(`已添加商品 ${product.name} 到购物`) } else { console.log('商品不存在') } } // 从购物中移除商品 function removeFromCart(id) { const index = cart.findIndex(p => p.id === parseInt(id)) if (index !== -1) { const product = cart.splice(index, 1)[0] console.log(`已从购物中移除商品 ${product.name}`) } else { console.log('购物中不存在该商品') } } // 计算购物总价 function getTotalPrice() { return cart.reduce((total, product) => total + product.price, 0) } // 打印购物商品列表和总价 function printCart() { console.log('购物商品列表:') cart.forEach(product => console.log(`- ${product.name}(${product.price}元)`)) console.log(`总价:${getTotalPrice()}元`) } // 测试 addToCart(1) addToCart(2) addToCart(3) removeFromCart(2) printCart() ``` 在这个示例中,我们首先定义了一个商品列表和一个购物数组,然后实现了三个操作函数: - `addToCart(id)`:将指定id的商品添加到购物中; - `removeFromCart(id)`:从购物中移除指定id的商品; - `getTotalPrice()`:计算购物中所有商品的总价。 最后,我们调用了这些函数来测试程序的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值