Day29--課后作業(yè)(購物車)

CSS代碼:

<style>
            * { 
                margin: 0;
                padding: 0;
            }
            body {
                width: 960px;
                margin: 20px auto;
            }
            #cart {
                margin: 0 auto;
                width: 850px;
            }
            #cart-header {
                height: 40px;
                background-color: lightgray;
                margin-bottom: 20px;
            }
            #cart-header div {
                line-height: 40px;
            }
            .left {
                float: left;
            }
            .right {
                float: right;
            }
            .w110 {
                width: 100px;
            }
            .ml10 {
                margin-left: 10px;
            }
            .w120 {
                width: 120px;
            }
            .w250 {
                width: 250px;
            }
            .center {
                text-align: center;
            }
            .w20 {
                width: 20px;
            }
            .w90 {
                width: 90px;
            }
            .clear {
                clear: both;
            }
            #cart-items>div {
                height: 100px;
            }
            #cart-items>div>div {
                line-height: 100px;
            }
            .w250 span {
                display: inline-block;
                font-size: 12px;
                line-height: 16px !important;
            }
            .single-item {
                border-bottom: 1px solid gray;
            }
            .small-button{
                display: inline-block;
                width: 20px;
                height: 20px;
                border: none;
            }
            .big-button {
                color: white;
                background-color: red;
                display: inline-block;
                width: 120px;
                height: 40px;
                border: none;
                font-size: 22px;
            }
            #totalCount, #totalPrice {
                color: red;
            }
            #totalPrice {
                font: bolder 20px Arial;
                display: inline-block;
                width: 150px;
            }
            #cart a {
                text-decoration: none;
            }
            #cart a:link, #cart a:visited, #cart a:active {
                color: gray;
            }
        </style>

HTML代碼:

<body>
        <div id="cart">
            <div id="cart-header">
                <div class="left w110 ml10">
                    <input id="selectAll" type="checkbox">
                    <label for="selectAll">全選</label>
                </div>
                <div class="left w250">商品</div>
                <div class="left w120 center">單價(jià)</div>
                <div class="left w120 center">數(shù)量</div>
                <div class="left w120 center">小計(jì)</div>
                <div class="left w120 center">操作</div>
            </div>
            <div id="cart-items">
                <div class="clear single-item">
                    <div class="left w20 ml10">
                        <input name="selectOne" type="checkbox">
                    </div>
                    <div class="left w90">
                        <a href="">
                            <img src="img/a1.jpg">
                        </a>
                    </div>
                    <div class="left w250">
                        <span>
                        海瀾之家/Heilan Home春裝商務(wù)白襯衫男修身HNCAD3A067Y 漂白(69) 漂
                        </span>
                    </div>
                    <div class="left w120 center">&yen;<span class="price">138.00</span></div>
                    <div class="left w120 center">
                        <button class="small-button">-</button>
                        <input class="center count" type="text" size="2" value="1">
                        <button class="small-button">+</button>
                    </div>
                    <div class="left w120 center">&yen;<span>138.00</span></div>
                    <div class="left w120 center">
                        <a href="javascript:void(0);">刪除</a>
                    </div>
                </div>
                <div class="clear single-item">
                    <div class="left w20 ml10">
                        <input name="selectOne" type="checkbox">
                    </div>
                    <div class="left w90">
                        <a href="">
                            <img src="img/a2.jpg">
                        </a>
                    </div>
                    <div class="left w250">
                        <span>
                        HLA海瀾之家長(zhǎng)袖襯衫男牛津紡休閑干凈透氣HNEAJ1E048A淺灰
                        </span>
                    </div>
                    <div class="left w120 center">&yen;<span class="price">128.00</span></div>
                    <div class="left w120 center">
                        <button class="small-button">-</button>
                        <input class="center count" type="text" size="2" value="1">
                        <button class="small-button">+</button>
                    </div>
                    <div class="left w120 center">&yen;<span>128.00</span></div>
                    <div class="left w120 center">
                        <a href="javascript:void(0);">刪除</a>
                    </div>
                </div>
                <div class="clear single-item">
                    <div class="left w20 ml10">
                        <input name="selectOne" type="checkbox">
                    </div>
                    <div class="left w90">
                        <a href="">
                            <img src="img/a3.jpg">
                        </a>
                    </div>
                    <div class="left w250">
                        <span>
                        HLA海瀾之家牛津紡清新休閑襯衫2018春季新品質(zhì)感柔軟長(zhǎng)袖襯衫男
                        </span>
                    </div>
                    <div class="left w120 center">&yen;<span class="price">99.00</span></div>
                    <div class="left w120 center">
                        <button class="small-button">-</button>
                        <input class="center count" type="text" size="2" value="1">
                        <button class="small-button">+</button>
                    </div>
                    <div class="left w120 center">&yen;<span>99.00</span></div>
                    
                    <div class="left w120 center">
                        <a href="javascript:void(0);">刪除</a>
                    </div>
                </div>
            </div>
            <div id="cart-footer">
                <div class="clear left">
                    <a id="clearSelected" href="javascript:void(0);">刪除選中商品</a>
                </div>
                <div class="right">
                    <span>總共選中了<span id="totalCount">0</span>件商品</span>
                    <span>總計(jì): <span id="totalPrice">&yen;0.00</span></span>
                    <button id="pay" class="big-button">去結(jié)算</button>
                </div>
            </div>
        </div>
</body>

JS 代碼:

<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
        <script>
            $(function(){
                
                
                //初始化
                $("#cart-items input[name='selectOne']").each(function(){
                    var price=$(this).parent().parent().children()[5].children[0].textContent;
                    var count=$(this).parent().parent().children()[4].children[1].value;
                    $(this).attr({"totalPrice":price,"count":count});   
                });
                
                showInfo();
                // 打印商品總計(jì)和總價(jià)
                function showInfo(){
                    var totalCount=0;
                    var totalPrice=0;
                    var selectOnelist=$("#cart-items input[name='selectOne']");
                    if(selectOnelist.length==0){
                        $("#totalCount").text("0");
                        $("#totalPrice").text("¥"+"0.00");
                    }
                    for (i=0;i<selectOnelist.length;i+=1){
                        var price=$(selectOnelist[i]).attr("totalPrice");
                        var count=$(selectOnelist[i]).attr("count");
                        if (selectOnelist[i].checked){
                            totalCount+=parseFloat(count);
                            totalPrice+=parseFloat(price);
                        }
                        $("#totalCount").text(totalCount);
                        $("#totalPrice").text("¥"+totalPrice.toFixed(2));
                    }
                }
                // 全選按鈕
                $("#selectAll").on("click",function(){
                    if($("#selectAll").get(0).checked){
                        $("input[name='selectOne']").each(function(){
                            this.checked=true;
                        });
                    }
                    else{
                        $("input[name='selectOne']").each(function(){
                            this.checked=false;
                        });
                    }
                    showInfo();
                })
                // 數(shù)量減少按鈕
                $("#cart-items button.small-button:even").on("click",function(evt){
                    var num=parseInt($(evt.target).next().val());
                    if(num==0){
                        $(evt.target).next().val(num);
                    }
                    else{
                        $(evt.target).next().val(num-1);
                    }
                    // 計(jì)算小計(jì)
                    var centerPrice=$(evt.target).parent().next().children();
                    var centerNum=$(evt.target).next().val();
                    var singlePrice=$(evt.target).parent().prev().children(".price").text();
                    centerPrice.text((parseFloat(singlePrice)*centerNum).toFixed(2));
                    // 改變selectOne的屬性
                    var selectOne=$(evt.target).parent().parent().children()[0].children;
                    $(selectOne).attr({"totalPrice":centerPrice.text(),"count":$(evt.target).next().val()});
                    // 修改顯示
                    showInfo();

                })
                // 數(shù)量增加按鈕
                $("#cart-items button.small-button:odd").on("click",function(evt){
                    var num=parseInt($(evt.target).prev().val());
                    if(num==200){
                        $(evt.target).prev().val(num);
                    }
                    else{
                        $(evt.target).prev().val(num+1);
                    }
                    // 計(jì)算小計(jì)
                    var centerPrice=$(evt.target).parent().next().children();
                    var centerNum=$(evt.target).prev().val();
                    var singlePrice=$(evt.target).parent().prev().children(".price").text();
                    centerPrice.text((parseFloat(singlePrice)*centerNum).toFixed(2));                   
                    // 改變selectOne的屬性
                    var selectOne=$(evt.target).parent().parent().children()[0].children;
                    $(selectOne).attr({"totalPrice":centerPrice.text(),"count":$(evt.target).prev().val()});
                    //修改顯示
                    showInfo();
                })
                // 復(fù)選框
                $("#cart-items input[name='selectOne']").on("click",function(evt){
                    showInfo(); 
                })
                // 刪除按鈕
                $("#cart-items a").on("click",function(evt){
                    var confirm=window.confirm("are you sure?");
                    if (confirm){
                        $(evt.target).parent().parent().remove();
                    }
                    showInfo();
                })
                // 刪除選中按鈕
                $("a[id='clearSelected']").on("click",function(){
                    var confirm=window.confirm("are you sure?");
                    if (confirm){
                        $("#cart-items input[name='selectOne']").each(function(){
                        if($(this).get(0).checked){
                            $(this).parent().parent().remove();
                        }
                    });
                    }
                    showInfo();
                })
                
            });
            
        
        </script>

效果:


1.png
2.png
3.png
4.png
5.png
6.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末佩伤,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌膘盖,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件斤蔓,死亡現(xiàn)場(chǎng)離奇詭異孵滞,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)遏餐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來赢底,“玉大人失都,你說我怎么就攤上這事⌒叶常” “怎么了粹庞?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)洽损。 經(jīng)常有香客問我庞溜,道長(zhǎng),這世上最難降的妖魔是什么碑定? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任流码,我火速辦了婚禮,結(jié)果婚禮上延刘,老公的妹妹穿的比我還像新娘旅掂。我一直安慰自己,他們只是感情好访娶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布商虐。 她就那樣靜靜地躺著觉阅,像睡著了一般。 火紅的嫁衣襯著肌膚如雪秘车。 梳的紋絲不亂的頭發(fā)上典勇,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音叮趴,去河邊找鬼割笙。 笑死,一個(gè)胖子當(dāng)著我的面吹牛眯亦,可吹牛的內(nèi)容都是我干的伤溉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼妻率,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼乱顾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起宫静,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤走净,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后孤里,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體伏伯,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年捌袜,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了说搅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡虏等,死狀恐怖弄唧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情博其,我是刑警寧澤套才,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布迂猴,位于F島的核電站慕淡,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏沸毁。R本人自食惡果不足惜峰髓,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望息尺。 院中可真熱鬧携兵,春花似錦、人聲如沸搂誉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至并级,卻和暖如春拂檩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嘲碧。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工稻励, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人愈涩。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓望抽,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親履婉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子煤篙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)谐鼎。 注意:講述HT...
    kismetajun閱讀 27,489評(píng)論 1 45
  • 1 Webpack 1.1 概念簡(jiǎn)介 1.1.1 WebPack是什么 1舰蟆、一個(gè)打包工具 2、一個(gè)模塊加載工具 3...
    Kevin_Junbaozi閱讀 6,673評(píng)論 0 16
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案狸棍? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 13,754評(píng)論 1 92
  • 1身害、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 15,982評(píng)論 3 119
  • 【一】引入與小括號(hào)的復(fù)習(xí) * 故事引入 師:板書18 2 3 生:三個(gè)數(shù),三個(gè)數(shù)字 師:這是三個(gè)什么數(shù)字 生:...
    LC13579閱讀 2,750評(píng)論 0 0