HTML+CSS实现购物车表格

HTML:

<main>
        <div class="container">
            <table border="0" cellspacing = "0" id = "shoppingcartinfo">
                <thead>                
                    <tr>
                        <th width = "75px" height = "42px">
                            <label for="checkall">
                                <input type="checkbox" name="checkall" id="checkall" class="checkall">
                                <span></span>全选
                            </label>
                        </th>
                        <th width = "400px" height = "42px">商品</th>
                        <th width = "135px" height = "42px">单价</th>
                        <th width = "230px" height = "42px">购买数量</th>
                        <th width = "150px" height = "42px">小计</th>
                        <th width = "200px" height = "42px">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td width = "75px" height = "90px" colspan="2" align="left" valign = "top" class="pt20">
                            <label for="checkall1">
                                <input type="checkbox" name="checkall" id="checkall1" class="checkall">
                                <span class = "checkallspan fl"></span>
                            </label>
                            <img src="../images/k5.webp" alt="cosmetics" class ="shoppingitem fl"> 
                            <div class="shoppingdesc fl">
                                Texamo黛末烁影系列26支化妆刷子套装眼
                                <br><br>
                                <b>颜色分类:粉红&nbsp;&nbsp;刷毛材质:人造纤维</b>
                            </div>
                            <div style="clear: both;"></div>
                        </td>
                        <td width = "135px" height = "90px" align="center" valign="top" class="price pt20">¥109.00</td>
                        <td width = "230px" height = "90px" align="center" valign="top" class="purchasenum pt20">
                            <i class="minus iconfont icon-subtract"></i>
                            <input type="number" min="1" step="1" value="1">
                            <i class="plus iconfont icon-jiahao"></i>
                        </td>
                        <td width = "150px" height = "90px" align="center" valign="top" class="pt20 sumup">¥318.00</td>
                        <td width = "115px" height = "90px" align="left" valign="top" class="pt20 pl85 operation">
                            <strong>删除</strong><br>
                            <strong>移入收藏夹</strong>
                        </td>
                    </tr>
                    <tr>
                        <td width = "75px" height = "90px" colspan="2" align="left" valign = "top" class="pt20">
                            <label for="checkall2">
                                <input type="checkbox" name="checkall" id="checkall2" class="checkall">
                                <span class = "checkallspan fl"></span>
                            </label>
                            <img src="../images/hot_item3.jpg" alt="laundry" class ="shoppingitem fl"> 
                            <div class="shoppingdesc fl">
                                 白猫无磷洗衣液3kg*4瓶整箱装
                                <br><br>
                                <b>颜色分类:蓝色&nbsp;&nbsp;版本:洁净柔香</b>
                            </div>
                            <div style="clear: both;"></div>
                        </td>
                        <td width = "135px" height = "90px" align="center" valign="top" class="price pt20">¥20.00</td>
                        <td width = "230px" height = "90px" align="center" valign="top" class="purchasenum pt20">
                            <i class="minus iconfont icon-subtract"></i>
                            <input type="number" min="1" step="1" value="1">
                            <i class="plus iconfont icon-jiahao"></i>
                        </td>
                        <td width = "150px" height = "90px" align="center" valign="top" class="pt20 sumup">¥80.00</td>
                        <td width = "115px" height = "90px" align="left" valign="top" class="pt20 pl85 operation">
                            <strong>删除</strong><br>
                            <strong>移入收藏夹</strong>
                        </td>
                    </tr>
                    <tr>
                        <td width = "75px" height = "90px" colspan="2" align="left" valign = "top" class="pt20">
                            <label for="checkall3">
                                <input type="checkbox" name="checkall" id="checkall3" class="checkall">
                                <span class = "checkallspan fl"></span>
                            </label>
                            <img src="../images/hot_item7.jpg" alt="kettle" class ="shoppingitem fl"> 
                            <div class="shoppingdesc fl">
                                苏泊尔电热水壶家用不锈钢防烫大容量自动断电
                                <br><br>
                                <b>颜色分类:玫瑰金&nbsp;&nbsp;服务: 全面保修2年</b>
                            </div>
                            <div style="clear: both;"></div>
                        </td>
                        <td width = "135px" height = "90px" align="center" valign="top" class="price pt20">¥99.00</td>
                        <td width = "230px" height = "90px" align="center" valign="top" class="purchasenum pt20">
                            <i class="minus iconfont icon-subtract"></i>
                            <input type="number" min="1" step="1" value="1">
                            <i class="plus iconfont icon-jiahao"></i>
                        </td>
                        <td width = "150px" height = "90px" align="center" valign="top" class="pt20 sumup">¥99.00</td>
                        <td width = "115px" height = "90px" align="left" valign="top" class="pt20 pl85 operation">
                            <strong>删除</strong><br>
                            <strong>移入收藏夹</strong>
                        </td>
                    </tr>
                    <tr>
                        <td width = "75px" height = "90px" colspan="2" align="left" valign = "top" class="pt20">
                            <label for="checkall4">
                                <input type="checkbox" name="checkall" id="checkall4" class="checkall">
                                <span class = "checkallspan fl"></span>
                            </label>
                            <img src="../images/huaweiphone_02.png" alt="huaweiphone" class ="shoppingitem fl"> 
                            <div class="shoppingdesc fl">
                                HUAWEI nova8 SE 5G 超清四摄
                                <br><br>
                                <b>颜色分类:天空蓝&nbsp;&nbsp;版本:6GB+128GB</b>
                            </div>
                            <div style="clear: both;"></div>
                        </td>
                        <td width = "135px" height = "90px" align="center" valign="top" class="price pt20">¥2599.00</td>
                        <td width = "230px" height = "90px" align="center" valign="top" class="purchasenum pt20">
                            <i class="minus iconfont icon-subtract"></i>
                            <input type="number" min="1" step="1" value="1">
                            <i class="plus iconfont icon-jiahao"></i>
                        </td>
                        <td width = "150px" height = "90px" align="center" valign="top" class="pt20 sumup">¥2599.00</td>
                        <td width = "115px" height = "90px" align="left" valign="top" class="pt20 pl85 operation">
                            <strong>删除</strong><br>
                            <strong>移入收藏夹</strong>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="paddingpanel clearfix">
                <div class="conclude clearfix">
                    <div class="concludeleft fl">
                        <label for="checkall5">
                            <input type="checkbox" name="checkall" id="checkall5" class="checkall">
                            <span></span>全选
                        </label>
                        <a href="#">删除选中商品</a>
                    </div>
                    <div class="concluderight fr">
                        <span class="fl">已选择<strong>1</strong>件商品</span>
                        <dl class="fl">
                            <dt></dt>
                            <dd>总价:¥3096.00</dd>
                            <dd>优惠:¥200.00</dd>
                            <dd>合计:<strong>¥2886.00</strong></dd>
                        </dl>
                        <div style="clear:both"></div>
                    </div>
                </div>
                <div class="continueorsubmit fr">
                    <button type="button" class="continue">继续购物</button>
                    <button type="submit" class="submit">提交订单</button>
                </div>
                
            </div>
        </div>
    </main>

CSS:

.fl{
    float: left;
}
.fr{
    float: right;
}
#shoppingcartinfo thead{
    background-color: #f5f5f5;
    /* border: 1px solid #dcdcdc; */
}

#shoppingcartinfo tr th{
    font: 12px/1 "宋体";
    color:#666666;
}
#shoppingcartinfo tr{
    border: 1px solid #dcdcdc;
}
#shoppingcartinfo tr:hover{
    background-color: #fef9f6;
}
main .container .checkall{
    display: none;
}
main .container .checkall[type=checkbox]+span{
    display: inline-block;
    width:14px;
    height: 14px;
    border: 1px solid #d7d7d7;
    color:#b5b5b5;
    vertical-align: bottom;
    margin-right: 12px;
    
}
main .container .checkall[type=checkbox]:checked+span:after{
    content:'\2714';
    display: inline-block;
    font-size: 14px;
    width:14px;
    height: 14px;
    border: 1px solid #F36B10;
    color:#F36B10;
    text-align: center;
}
main .container .checkallspan{
    display: inline-block;
    margin: 0px 22px 0 16px;
}
main .container .shoppingitem{
    margin: 0px 15px 0 6px;
    width: 70px;
    height: 70px;
}
main .container .shoppingdesc{
    font: 12px/20px "宋体";
}
main .container .shoppingdesc > b{
    color:#888888;
}
main .container .price{
    font:14px/1 "Arial";
}
main .container .purchasenum > i,
main .container .purchasenum > input{
    display: inline-block;
    width: 30px;
    height:30px;
    line-height: 30px;
    text-align: center;
    color:#bbbbbb;
    border: 1px solid #cecece;
}
main .container .purchasenum > input{
    width:45px;
    box-sizing: content-box;
    vertical-align: top;
    margin-left: -4px;
    border-left-style: none;
}
main .container .purchasenum .plus{
    margin-left: -4px;
}
main .container .sumup{
    font: 16px/1 "Arial";
    color:#F36B10;
}
main .container .operation{
    font: 12px/25px "宋体";
    cursor: pointer;
}
main .container .operation strong:hover{
    color:#F36B10;
    text-decoration: underline;
}

 

  • 9
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的购物车表格的示例代码,包括加减按钮和总价小计功能: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>购物车</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: center; padding: 8px; border: 1px solid #ddd; } th { background-color: #f2f2f2; } .total { font-weight: bold; } </style> </head> <body> <table> <thead> <tr> <th>商品名称</th> <th>单价</th> <th>数量</th> <th>小计</th> </tr> </thead> <tbody> <tr> <td>商品1</td> <td>10.00</td> <td> <button class="reduce">-</button> <span class="amount">1</span> <button class="add">+</button> </td> <td class="subtotal">10.00</td> </tr> <tr> <td>商品2</td> <td>20.00</td> <td> <button class="reduce">-</button> <span class="amount">1</span> <button class="add">+</button> </td> <td class="subtotal">20.00</td> </tr> <tr> <th colspan="3" class="total">总价</th> <td id="totalPrice">30.00</td> </tr> </tbody> </table> <script> // 获取元素 const reduceBtns = document.querySelectorAll('.reduce'); const addBtns = document.querySelectorAll('.add'); const amountEles = document.querySelectorAll('.amount'); const subtotalEles = document.querySelectorAll('.subtotal'); const totalPriceEle = document.querySelector('#totalPrice'); // 定义变量 let totalPrice = 0; // 绑定事件 for (let i = 0; i < reduceBtns.length; i++) { reduceBtns[i].addEventListener('click', function() { let amount = parseInt(amountEles[i].textContent); if (amount > 1) { amount--; amountEles[i].textContent = amount; let price = parseFloat(subtotalEles[i].textContent) / (amount + 1); subtotalEles[i].textContent = (price * amount).toFixed(2); totalPrice = parseFloat(totalPrice) - price; totalPriceEle.textContent = totalPrice.toFixed(2); } }); } for (let i = 0; i < addBtns.length; i++) { addBtns[i].addEventListener('click', function() { let amount = parseInt(amountEles[i].textContent); amount++; amountEles[i].textContent = amount; let price = parseFloat(subtotalEles[i].textContent) / (amount - 1); subtotalEles[i].textContent = (price * amount).toFixed(2); totalPrice = parseFloat(totalPrice) + price; totalPriceEle.textContent = totalPrice.toFixed(2); }); } // 计算初始总价 for (let i = 0; i < subtotalEles.length; i++) { totalPrice += parseFloat(subtotalEles[i].textContent); } totalPriceEle.textContent = totalPrice.toFixed(2); </script> </body> </html> ``` 代码解释: 1. HTML部分:使用`<table>`标签创建表格,包括表头和表体两部分。每个商品行都有一个数量`<td>`单元格,里面包含一个减少按钮、一个显示数量的`<span>`元素和一个增加按钮。总价小计使用`<td>`标记,总价使用`<td>`标记和一个`id`属性。 2. CSS部分:设置表格样式,包括边框、对齐和背景色。 3. JavaScript部分:获取所有需要操作的元素,并定义一个变量`totalPrice`用于统计总价。为减少按钮和增加按钮绑定`click`事件,用于修改数量和小计,并更新总价。在页面加载时计算初始总价。 以上代码仅供参考,实际应用中可能需要根据实际情况进行调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值