JS&JQ添加购物车功能

啥也不说。直接上代码!!!!
有小伙伴想要代码!全部代码在最后放
在这里插入图片描述
第一步。引入script src=“jquery-3.3.1.min.js”
第二步。先写一个$ (function() {})函数。这里要说一下。这个$(function() {})是最大的函数。包住全部。下面的图片是加入购物车。再下面是分部

在这里插入图片描述

下面这些才是函数里面的东西、

添加购物车!!!!

在这里插入图片描述

第三步。先找到加入购物车的a标签。然后分别找到a标签父亲的兄弟的文本——图片、商品名称、折扣价

在这里插入图片描述
在这里插入图片描述

//获取购物车标签#goods tr td a
$('#goods tr td a').click(function() 
{
		//获取a标签父亲的第二个兄弟的文本——图片
		var tup = $(this).parent().siblings().eq(1).html();
		//获取a标签父亲的第二个兄弟的文本——商品名称
		var name = $(this).parent().parent().children().eq(2).html();
		//获取a标签父亲的第二个兄弟的文本——折扣价
		var jin = $(this).parent().siblings().eq(3).html();

这是判重。还是一个点击事件的代码

		//判重判断商品在购物车里是否存在
		var flag = true;
		$('#cart tr').each(function() 
		{
			//判断name在新添加的name是否一致。这里的name是上面获取的
			if ($(this).children().eq(2).text() == name) 
			{
				//已经存在
				flag = false;
				//更新原来的数量
				var num = parseFloat($(this).children().eq(3).text());
				//更新原来的小计
				var ji = parseFloat($(this).children().eq(5).text());
				//计算数量
				$(this).children().eq(3).text(num + 1);
				//计算小计
				$(this).children().eq(5).text((num + 1) * jin);
			}
		});
		//创建7个单元格
		if (flag)
	    {
			var t1 = "<td><input type='checkbox'></td>";//选中
			var tp = "<td>" + tup + "</td>";//图片
			var mc = "<td>" + name + "</td>";//名称
			var shu = "<td>" + 1 + "</td>"//数量
			var jg = "<td>" + jin + "</td>"//价格
			var xj = "<td>" + jin + "</td>"//小计
			var sc = "<td><a href='#'>删除</a></td>"//删除按钮
			//创建行
			var tr = 
			"<tr>" 
				+ t1 + "" + tp + "" + mc + ""
				+ shu + "" + jg + "" + xj + "" 
				+ sc + 
			"</tr>"
			//追加这一行
			$("#cart").append(tr);
		}
		//增加 数量和总计
		var num = 0;//数量
		var sum = 0;//小计
		//购物车行数循环
		$("#cart tr").each(function() 
		{
			//购物车里面的 【数量】的文本。数量变化
			num += parseFloat($(this).children().eq(3).text());
			//购物车里面的【小计】的文本。小计就变化,价格不变
			sum += parseFloat($(this).children().eq(5).text());
		});
		//从上面找到他们的id
		$("#allNum").text(num);
		$("#allMoney").text(sum);
});

#cart就是添加购物车商品追加的容器。tr就是刚才创建的行

在这里插入图片描述

到这里添加就完成了!!!
下面就是全选

在这里插入图片描述

‘#carsTable input[type=“checkbox”]’——就是全选按钮
‘#cart input[type=“checkbox”]’——就是单选选中按钮

在这里插入图片描述

反选。中间有bug。不过小事情

在这里插入图片描述

删除!!

在这里插入图片描述

在这里插入图片描述

结束!拜拜!!!

<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script src="./jquery-3.3.1.min.js"></script>
    <style type="text/css">
        * {
            margin: 0px;
            padding: 0px;
        }
        
        table {
            width: 1000px;
            border: 1px solid red;
            margin: 0px auto;
            border-collapse: collapse;
            text-align: center;
        }
        
        td,
        th {
            border: 1px solid red;
            height: 40px;
        }
        /* 对话框的样式设置 */
        /*遮罩层  */
        
        #mask {
            background-color: gray;
            position: absolute;
            left: 0;
            top: 0;
            z-index: 100;
            opacity: 0.3;
            filter: alpha(opacity=30);
        }
        /*对话框整体布局  */
        
        #dialog {
            background-color: #fff;
            border: 3px solid orange;
            height: 200px;
            position: absolute;
            width: 300px;
            z-index: 101;
            display: none;
            position: absolute;
            left: 600px;
            top: 300px;
        }
        /*对话框头部背景  */
        
        #dialog div#title {
            background: #cccccc;
            overflow: hidden;
        }
        /*对话框头部中的关闭按钮布局  */
        
        #dialog p.close {
            float: right;
            padding-right: 10px;
            height: 24px;
            line-height: 24px;
            width: 40px;
            cursor: pointer;
            color: red;
        }
        /* 对话框中提示部分的布局 */
        
        #dialog div#infor {
            text-align: center;
            height: 80px;
            line-height: 80px;
        }
        /*对话框中确认按钮的布局  */
        
        #dialog div#sure {
            text-align: center;
            height: 30px;
            line-height: 30px;
        }
        /* 商品图片大小 */
        
        table img {
            width: 90px;
            height: 80px;
        }
        /* 结账按钮 */
        
        #buyAll {
            width: 120px;
            height: 50px;
            color: red;
            font-size: 30px;
        }
        
        table a {
            text-decoration: none;
            color: red;
        }
        
        table a :HOVER {
            color: blue;
        }
        
        .newClass {
            border: 5px solid yellow;
        }
        
        #menuDiv {
            height: 100px;
            margin: 0 auto;
            width: 800px;
        }
        
        #menuDiv a {
            text-decoration: none;
            color: black;
        }
        
        #menuDiv a:HOVER {
            color: orange;
        }
        
        #menuDiv ul {
            list-style: none;
            position: absolute;
        }
        
        #menuDiv ul li {
            width: 100px;
            height: 40px;
            line-height: 40px;
            float: left;
            border: 1px solid red;
            text-align: center;
            position: relative;
        }
        
        #menuDiv li.menuList ul {
            display: none;
        }
        
        caption {
            color: black;
            font-size: 30px;
            margin-bottom: 10px;
        }
        
        button {
            width: 90px;
            height: 30px;
            border-width: 0px;
            border-radius: 3px;
            background: #1E90FF;
            cursor: pointer;
            outline: none;
            font-family: Microsoft YaHei;
            color: white;
            font-size: 17px;
        }
        
        button:HOVER {
            background: #5599FF;
        }
        .f{
            position: absolute;
            display:none;
        }
    </style>
    
</head>

<body>
    <div id="menuDiv">
        <ul>
            <li><a href="#">首页 <ul class="f"><li>首页</li></ul></a></li>
            <li><a href="#">家纺家居<ul class="f"><li>首页</li></ul></a></li>
            <li><a href="#">智能家电<ul class="f"><li>首页</li></ul></a></li>
            <li><a href="#">手机电脑<ul class="f"><li>首页</li></ul></a></li>
            <li><a href="#">食品生鲜<ul class="f"><li>首页</li></ul></a></li>
        </ul>
    </div>

    <table id="goods">
        <caption>双十一选购会</caption>
        <tr>
            <th>编号</th>
            <th>图片</th>
            <th>商品名称</th>
            <th>折扣价(¥)</th>
            <th>抢抢抢!</th>
        </tr>
        <tr>
            <td>1001</td>
            <td><img src="../imgs/goods1.png"></img>
            </td>
            <td>闪迪64G存储卡行车记录仪</td>
            <td>74</td>
            <td><a href="#">加入购物车</a>
            </td>
        </tr>
        <tr>
            <td>1002</td>
            <td><img src="../imgs/goods2.png"></img>
            </td>
            <td>天际隔水电炖锅</td>
            <td>119</td>
            <td><a href="#">加入购物车</a>
            </td>
        </tr>
        <tr>
            <td>1003</td>
            <td><img src="../imgs/goods3.png"></img>
            </td>
            <td>创维55寸大屏电视</td>
            <td>3699</td>
            <td><a href="#">加入购物车</a>
            </td>
        </tr>
        <tr>
            <td>1004</td>
            <td><img src="../imgs/goods4.png"></img>
            </td>
            <td>美玲电冰箱</td>
            <td>2999</td>
            <td><a href="#">加入购物车</a>
            </td>
        </tr>
        <tr>
            <td>1005</td>
            <td><img src="../imgs/goods5.png"></img>
            </td>
            <td>大疆MAVICPRO无人机</td>
            <td>3000</td>
            <td><a href="#">加入购物车</a>
            </td>
        </tr>
    </table>
    <table id="carsTable" style="margin-top: 90px">
        <caption>购物车</caption>
        <thead>
            <tr>
                <th><input type="checkbox" />全选</th>
                <th>图片</th>
                <th>名称</th>
                <th>数量</th>
                <th>价格</th>
                <th>小计</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="cart">
        </tbody>
        <tfoot>
            <tr>
                <td colspan="6">数量是:<span id="allNum">0</span>个 价格是:¥<span id="allMoney">0</span>元</td>
                <td colspan="1"><button id="dellAll">批量删除</button></td>
            </tr>
        </tfoot>
    </table>
    <script>
        $(function() {
            //获取a标签
            var $li = $("#menuDiv>ul>li>a");
           //给li注册鼠标经过事件,让自己的ul显示出来
            $li.mouseenter(function () {
                //找到所有的儿子,并且还得是ul
                $(this).children("ul").stop().slideDown(200);
            });
        
            $li.mouseleave(function () {
                $(this).children("ul").stop().slideUp(200);
            });
        })
        $(function() {
            //获取购物车标签#goods tr td a
            $('#goods tr td a').click(function() {
                //获取a标签父亲的第二个兄弟的文本——图片
                var tup = $(this).parent().siblings().eq(1).html();
                //获取a标签父亲的第二个兄弟的文本——商品名称
                var name = $(this).parent().parent().children().eq(2).html();
                //获取a标签父亲的第二个兄弟的文本——折扣价
                var jin = $(this).parent().siblings().eq(3).html();
                //判重判断商品在购物车里是否存在
                var flag = true;
                $('#cart tr').each(function() {
                    //判断name在新添加的name是否一致
                    if ($(this).children().eq(2).text() == name) {
                        //已经存在
                        flag = false;
                        //更新原来的数量和小计
                        var num = parseFloat($(this).children().eq(3).text());
                        var ji = parseFloat($(this).children().eq(5).text());
                        $(this).children().eq(3).text(num + 1);
                        $(this).children().eq(5).text((num + 1) * jin);
                    }
                });
                //创建7个单元格
                if (flag) {
                    var t1 = "<td><input type='checkbox'></td>";
                    var tp = "<td>" + tup + "</td>";
                    var mc = "<td>" + name + "</td>";
                    var shu = "<td>" + 1 + "</td>"
                    var jg = "<td>" + jin + "</td>"
                    var xj = "<td>" + jin + "</td>"
                    var sc = "<td><a href='#'>删除</a></td>"
                    var tr = "<tr>" + t1 + "" + tp + "" + mc + "" + shu + "" + jg + "" + xj + "" + sc + "</tr>"
                    $("#cart").append(tr);
                }
                //增加总计
                var num = 0;
                var sum = 0;
                $("#cart tr").each(function() {
                    num += parseFloat($(this).children().eq(3).text());
                    sum += parseFloat($(this).children().eq(5).text());
                });
                $("#allNum").text(num);
                $("#allMoney").text(sum);

            });
            //全选
            $('#carsTable input[type="checkbox"]').click(function() {
                    //attr,prop
                    if ($(this).prop('checked')) {
                        $('#cart input[type="checkbox"]').each(function() {
                            $(this).prop('checked', true)
                        });
                    } else {
                        $('#cart input[type="checkbox"]').each(function() {
                            $(this.prop('checked', false))
                        });
                    }
                })
                //反选
            $("#cart").on("click", "input[type='checkbox']", function() {
                    var flag = true;
                    $("#cart input[type='checkbox']").each(function() {
                        if ($(this).prop("checked") == false) {
                            $("#carsTable thead input[type='checkbox']").prop("checked", false);
                            flag = false;
                        }
                    });
                    if (flag) {
                        $("#carsTable thead input[type='checkbox']").prop("checked", true);
                    }
                })
                //删除
            $('#carsTable').on("click", "a", function() {
                $(this).parent().parent().remove();
                zz();
            });
            //批量删除
            $('#dellAll').click(function() {
                $("#cart input[type='checkbox']").each(function() {
                    if ($(this).prop('checked') == true) {
                        $(this).parent().parent().remove();
                        zz();
                    };
                });
            });
        });
    </script>
</body>

</html>

```javascript
在这里插入代码片

  • 10
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚来的棠棠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值