案例13-购物车-重要!

第一步:

第二步:闭包

第三步:完成剩余部分

 


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <link rel="stylesheet" href="./index.css">

    <title>Document</title>

</head>

<body>

    <div class="box">

        <div class="container">

            <ul id="list">

                <li>

                    <div class="list-left">

                        <input type="button" class="minus" value="-" />

                        <span class="n">0</span>

                        <input type="button" class="plus" value="+" />

                    </div>

                    <div class="list-right">

                        <span>单价:</span>

                        <em class="price">12.5</em>元

                        <span>小计:</span>

                        <em class="count">0</em>元

                    </div>

                </li>

                <li>

                    <div class="list-left">

                        <input type="button" class="minus" value="-" />

                        <span class="n">0</span>

                        <input type="button" class="plus" value="+" />

                    </div>

                    <div class="list-right">

                        <span>单价:</span>

                        <em class="price">13.5</em>元

                        <span>小计:</span>

                        <em class="count">0</em>元

                    </div>

                </li>

                <li>

                    <div class="list-left">

                        <input type="button" class="minus" value="-" />

                        <span class="n">0</span>

                        <input type="button" class="plus" value="+" />

                    </div>

                    <div class="list-right">

                        <span>单价:</span>

                        <em class="price">14.5</em>元

                        <span>小计:</span>

                        <em class="count">0</em>元

                    </div>

                </li>

                <li>

                    <div class="list-left">

                        <input type="button" class="minus" value="-" />

                        <span class="n">0</span>

                        <input type="button" class="plus" value="+" />

                    </div>

                    <div class="list-right">

                        <span>单价:</span>

                        <em class="price">15.5</em>元

                        <span>小计:</span>

                        <em class="count">0</em>元

                    </div>

                </li>

                <li>

                    <div class="list-left">

                        <input type="button" class="minus" value="-" />

                        <span class="n">0</span>

                        <input type="button" class="plus" value="+" />

                    </div>

                    <div class="list-right">

                        <span>单价:</span>

                        <em class="price">16.5</em>元

                        <span>小计:</span>

                        <em class="count">0</em>元

                    </div>

                </li>

            </ul>

            <p>商品合计共:<strong>0</strong>件,共花费了:<strong>0</strong>元</p>

            <p>其中最贵的已选商品单价是:<strong>0</strong>元</p>

        </div>

    </div>

    <script>

 

/**

 * 控制当前一列中的功能

 *  - 点击第一个 - 号,需要控制 数量发生改变(减少)。限制数量不能小于0

 *  - 点击第一个 + 号,需要控制 数量发生改变(增加)。限制数量不能超过10

 * 根据数量,计算小计结果

 * 

 * 1. 统计商品总的件数

 * 2. 统计花费的总数

 * 3. 从已选商品中,找出最贵的单价

 */

var plus = document.querySelectorAll('.plus');

var minus = document.querySelectorAll('.minus');

var ns = document.querySelectorAll('.n');

var price = document.querySelectorAll('.price');

var count = document.querySelectorAll('.count');

var strong = document.querySelectorAll('strong');

var len = ns.length;

//批量给元素添加事件

for(var i = 0;i<len;i++){

    todo(i);

}

function todo(index){

    //控制+号

    plus[index].onclick = function () {

        changeNum(index,+1);

    }

    //控制-号

    minus[index].onclick = function () {

        changeNum(index,-1);

    }

}

//控制数量变化的封装函数 , index指的是下标,change指的是加还是减

function changeNum(index,change){

    var num = +ns[index].innerHTML + change;

    switch(true){

        case num > 10:

            alert('最大限购10件');

            num = 10;

            return;

        case num < 0:

            alert('已经不能再少了哦');

            num = 0;

            return;

    }

    ns[index].innerHTML = num;

    count[index].innerHTML = num * (price[index].innerHTML * 10) / 10;

    total();

}

//求总数

function total(){

    //总件数

    var subTotal = 0;

    //总花费

    var numTotal = 0;

    //找到已选商品中,单价最高的

    var max = 0;

    for(var i=0;i<len;i++){

        subTotal += +ns[i].innerHTML;

        numTotal += +count[i].innerHTML;

        if(ns[i].innerHTML != 0){

            console.log(price[i].innerHTML);

            // if (price[i].innerHTML>max){

            //     max = price[i].innerHTML;

            // }

            max = price[i].innerHTML > max ? price[i].innerHTML:max;

        }

    }

    strong[0].innerHTML = subTotal;

    strong[1].innerHTML = numTotal;

    strong[2].innerHTML = max;

}

    </script>

</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值