日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

前端经典案例——购物车,Jquery实现

發布時間:2024/10/14 HTML 132 豆豆
生活随笔 收集整理的這篇文章主要介紹了 前端经典案例——购物车,Jquery实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Jquey實現

品優購案例
giteee項目地址
https://gitee.com/lyh1999/shopping_cart

<!DOCTYPE html> <html lang="zh-CN"><head><meta charset="UTF-8"><title>我的購物車-品優購</title><meta name="description" content="品優購JD.COM-專業的綜合網上購物商城,銷售家電、數碼通訊、電腦、家居百貨、服裝服飾、母嬰、圖書、食品等數萬個品牌優質商品.便捷、誠信的服務,為您提供愉悅的網上購物體驗!" /><meta name="Keywords" content="網上購物,網上商城,手機,筆記本,電腦,MP3,CD,VCD,DV,相機,數碼,配件,手表,存儲卡,品優購" /><!-- 引入facicon.ico網頁圖標 --><link rel="shortcut icon" href="favicon.ico" type="image/x-icon" /><!-- 引入css 初始化的css 文件 --><link rel="stylesheet" href="css/base.css"><!-- 引入公共樣式的css 文件 --><link rel="stylesheet" href="css/common.css"><!-- 引入car css --><link rel="stylesheet" href="css/car.css"><!-- 先引入jquery --><script src="js/jquery.min.js"></script><!-- 在引入我們自己的js文件 --><script src="js/car.js"></script> </head><body><!-- 頂部快捷導航start --><div class="shortcut"><div class="w"><div class="fl"><ul><li>品優購歡迎您! </li><li><a href="#">請登錄</a><a href="#" class="style-red">免費注冊</a></li></ul></div><div class="fr"><ul><li><a href="#">我的訂單</a></li><li class="spacer"></li><li><a href="#">我的品優購</a><i class="icomoon">?</i></li><li class="spacer"></li><li><a href="#">品優購會員</a></li><li class="spacer"></li><li><a href="#">企業采購</a></li><li class="spacer"></li><li><a href="#">關注品優購</a> <i class="icomoon">?</i></li><li class="spacer"></li><li><a href="#">客戶服務</a> <i class="icomoon">?</i></li><li class="spacer"></li><li><a href="#">網站導航</a> <i class="icomoon">?</i></li></ul></div></div></div><!-- 頂部快捷導航end --><div class="car-header"><div class="w"><div class="car-logo"><img src="img/logo.png" alt=""> <b>購物車</b></div></div></div></div><div class="c-container"><div class="w"><div class="cart-filter-bar"><em>全部商品</em></div><!-- 購物車主要核心區域 --><div class="cart-warp"><!-- 頭部全選模塊 --><div class="cart-thead"><div class="t-checkbox"><input type="checkbox" name="" id="" class="checkall"> 全選</div><div class="t-goods">商品</div><div class="t-price">單價</div><div class="t-num">數量</div><div class="t-sum">小計</div><div class="t-action">操作</div></div><!-- 商品詳細模塊 --><div class="cart-item-list"><div class="cart-item check-cart-item"><div class="p-checkbox"><input type="checkbox" name="" id="" checked class="j-checkbox"></div><div class="p-goods"><div class="p-img"><img src="upload/p1.jpg" alt=""></div><div class="p-msg">【5本26.8元】經典兒童文學彩圖青少版八十天環游地球中學生語文教學大綱</div></div><div class="p-price">¥12.60</div><div class="p-num"><div class="quantity-form"><a href="javascript:;" class="decrement">-</a><input type="text" class="itxt" value="1"><a href="javascript:;" class="increment">+</a></div></div><div class="p-sum">¥12.60</div><div class="p-action"><a href="javascript:;">刪除</a></div></div><div class="cart-item"><div class="p-checkbox"><input type="checkbox" name="" id="" class="j-checkbox"></div><div class="p-goods"><div class="p-img"><img src="upload/p2.jpg" alt=""></div><div class="p-msg">【2000張貼紙】貼紙書 3-6歲 貼畫兒童 貼畫書全套12冊 貼畫 貼紙兒童 汽</div></div><div class="p-price">¥24.80</div><div class="p-num"><div class="quantity-form"><a href="javascript:;" class="decrement">-</a><input type="text" class="itxt" value="1"><a href="javascript:;" class="increment">+</a></div></div><div class="p-sum">¥24.80</div><div class="p-action"><a href="javascript:;">刪除</a></div></div><div class="cart-item"><div class="p-checkbox"><input type="checkbox" name="" id="" class="j-checkbox"></div><div class="p-goods"><div class="p-img"><img src="upload/p3.jpg" alt=""></div><div class="p-msg">唐詩三百首+成語故事全2冊 一年級課外書 精裝注音兒童版 小學生二三年級課外閱讀書籍</div></div><div class="p-price">¥29.80</div><div class="p-num"><div class="quantity-form"><a href="javascript:;" class="decrement">-</a><input type="text" class="itxt" value="1"><a href="javascript:;" class="increment">+</a></div></div><div class="p-sum">¥29.80</div><div class="p-action"><a href="javascript:;">刪除</a></div></div></div><!-- 結算模塊 --><div class="cart-floatbar"><div class="select-all"><input type="checkbox" name="" id="" class="checkall">全選</div><div class="operation"><a href="javascript:;" class="remove-batch"> 刪除選中的商品</a><a href="javascript:;" class="clear-all">清理購物車</a></div><div class="toolbar-right"><div class="amount-sum">已經選<em>1</em>件商品</div><div class="price-sum">總價: <em>¥12.60</em></div><div class="btn-area">去結算</div></div></div></div></div></div><!-- footer start --><div class="footer"><div class="w"><!-- mod_service --><div class="mod_service"><ul><li><i class="mod-service-icon mod_service_zheng"></i><div class="mod_service_tit"><h5>正品保障</h5><p>正品保障,提供發票</p></div></li><li><i class="mod-service-icon mod_service_kuai"></i><div class="mod_service_tit"><h5>正品保障</h5><p>正品保障,提供發票</p></div></li><li><i class="mod-service-icon mod_service_bao"></i><div class="mod_service_tit"><h5>正品保障</h5><p>正品保障,提供發票</p></div></li><li><i class="mod-service-icon mod_service_bao"></i><div class="mod_service_tit"><h5>正品保障</h5><p>正品保障,提供發票</p></div></li><li><i class="mod-service-icon mod_service_bao"></i><div class="mod_service_tit"><h5>正品保障</h5><p>正品保障,提供發票</p></div></li></ul></div><!-- mod_help --><div class="mod_help"><dl class="mod_help_item"><dt>購物指南</dt><dd> <a href="#">購物流程 </a></dd><dd> <a href="#">會員介紹 </a></dd><dd> <a href="#">生活旅行/團購 </a></dd><dd> <a href="#">常見問題 </a></dd><dd> <a href="#">大家電 </a></dd><dd> <a href="#">聯系客服 </a></dd></dl><dl class="mod_help_item"><dt>購物指南</dt><dd> <a href="#">購物流程 </a></dd><dd> <a href="#">會員介紹 </a></dd><dd> <a href="#">生活旅行/團購 </a></dd><dd> <a href="#">常見問題 </a></dd><dd> <a href="#">大家電 </a></dd><dd> <a href="#">聯系客服 </a></dd></dl><dl class="mod_help_item"><dt>購物指南</dt><dd> <a href="#">購物流程 </a></dd><dd> <a href="#">會員介紹 </a></dd><dd> <a href="#">生活旅行/團購 </a></dd><dd> <a href="#">常見問題 </a></dd><dd> <a href="#">大家電 </a></dd><dd> <a href="#">聯系客服 </a></dd></dl><dl class="mod_help_item"><dt>購物指南</dt><dd> <a href="#">購物流程 </a></dd><dd> <a href="#">會員介紹 </a></dd><dd> <a href="#">生活旅行/團購 </a></dd><dd> <a href="#">常見問題 </a></dd><dd> <a href="#">大家電 </a></dd><dd> <a href="#">聯系客服 </a></dd></dl><dl class="mod_help_item"><dt>購物指南</dt><dd> <a href="#">購物流程 </a></dd><dd> <a href="#">會員介紹 </a></dd><dd> <a href="#">生活旅行/團購 </a></dd><dd> <a href="#">常見問題 </a></dd><dd> <a href="#">大家電 </a></dd><dd> <a href="#">聯系客服 </a></dd></dl><dl class="mod_help_item mod_help_app"><dt>幫助中心</dt><dd><img src="upload/erweima.png" alt=""><p>品優購客戶端</p></dd></dl></div><!-- mod_copyright --><div class="mod_copyright"><p class="mod_copyright_links">關于我們 | 聯系我們 | 聯系客服 | 商家入駐 | 營銷中心 | 手機品優購 | 友情鏈接 | 銷售聯盟 | 品優購社區 | 品優購公益 | English Site | Contact U</p><p class="mod_copyright_info">地址:北京市昌平區建材城西路金燕龍辦公樓一層 郵編:100096 電話:400-618-4000 傳真:010-82935100 郵箱: zhanghj+itcast.cn <br> 京ICP備08001421號京公網安備110108007702</p></div></div></div><!-- footer end --> </body></html>

car.js部分

$(function() {// 1全選模塊// 1.1將全選按鈕checkall狀態賦值到三個小按鈕j-checkbox上$(".checkall").change(function() {// console.log($(this).prop('checked'));$('.j-checkbox, .checkall').prop('checked', $(this).prop('checked'))if ($(this).prop('checked')) {// 讓所有商品添加check-cart-item類名$('.cart-item').addClass('check-cart-item')} else {$('.cart-item').removeClass('check-cart-item')}})// 1.2通過小按鈕選中全選按鈕$('.j-checkbox').change(function() {// 如果小按鈕全部選中,全選按鈕選中,否則不被選中// console.log($('.j-checkbox:checked').length);if ($('.j-checkbox:checked').length === $('.j-checkbox').length) {$('.checkall').prop('checked', true)} else {$('.checkall').prop('checked', false)}if ($(this).prop('checked')) {// 讓所有商品添加check-cart-item類名$(this).parents('.cart-item').addClass('check-cart-item')} else {$(this).parents('.cart-item').removeClass('check-cart-item')}})// 2商品模塊// 2.1商品數量增加// 點擊加號increment讓這個值++,賦值給文本框,只增加本商品數量兄弟文本框itxt值// 修改表單val(),在文本框原有數值上相加$('.increment').click(function() {// 得到當前兄弟文本框的值var n = $(this).siblings('.itxt').val();// console.log(n);n++;$(this).siblings('.itxt').val(n)// 2.1.1商品小計// 點擊加號或減號,根據文本框的數值乘以當前商品的價格,只計算當前商品小計(p-sum)// 修改普通元素內容方法text(),當前商品價格去除¥/$后再相乘,// 截取字符串substr(1)從第一個位置開始截取,保留兩位小數toFixed(2)// var p = $(this).parent().parent().siblings('.p-price').html();var p = $(this).parents(".p-num").siblings('.p-price').html();// console.log(p);p = p.substr(1);var price = (p * n).toFixed(2);$(this).parents(".p-num").siblings('.p-sum').html("¥" + price);// $(this).parent().parent().siblings('.p-sum').html("¥"+p * n)getSum();})// 2.2商品數量減少// 點擊加號decrement讓這個值--,賦值給文本框,只減少本商品數量兄弟文本框itxt值// 修改表單val(),在文本框原有數值上相減// 如果n值為1,不再減少$('.decrement').click(function() {// 得到當前兄弟文本框的值var n = $(this).siblings('.itxt').val();// console.log(n);if (n == 1) {return false}n--;$(this).siblings('.itxt').val(n)// 2.1.1商品小計// 點擊加號或減號,根據文本框的數值乘以當前商品的價格,只計算當前商品小計(p-sum)// 修改普通元素內容方法text(),當前商品價格去除¥/$后再相乘,截取字符串substr(1)從第一個位置開始截取var p = $(this).parents(".p-num").siblings('.p-price').html();// console.log(p);p = p.substr(1);var price = (p * n).toFixed(2);$(this).parents(".p-num").siblings('.p-sum').text("¥" + price);// $(this).parent().parent().siblings('.p-sum').html("¥"+p * n)getSum();})// 3用戶修改文本框的值計算小計$('.itxt').change(function() {// 先得到文本框的值乘以當前商品單價var n = $(this).val();var p = $(this).parents('.p-num').siblings('.p-price').html()p = p.substr(1);var price = (p * n).toFixed(2);$(this).parents(".p-num").siblings('.p-sum').html("¥" + price);getSum();})getSum();// 4總和總額function getSum() {var count = 0; //計算總件數var money = 0; //計算總價錢// 總件$('.itxt').each(function(i, ele) {count += parseInt($(ele).val());})$('.amount-sum em').text(count);// 總額$('.p-sum').each(function(i, ele) {money += parseFloat($(ele).text().substr(1));})$('.price-sum em').text("¥" + money.toFixed(2));}// 5刪除商品// 點擊按鈕刪除商品$('.p-action a').click(function() {$(this).parents('.cart-item').remove();getSum();});// 點擊刪除選中的商品$('.remove-batch').click(function() {// 刪除復選框選中的商品$('.j-checkbox:checked').parents('.cart-item').remove();getSum();});// 清空購物車$('.clear-all').click(function() {$('.cart-item').remove();getSum();})})

css部分
1base.css

/*清除元素默認的內外邊距 */ * {margin: 0;padding: 0 } /*讓所有斜體 不傾斜*/ em, i {font-style: normal; } /*去掉列表前面的小點*/ li {list-style: none; } /*圖片沒有邊框 去掉圖片底側的空白縫隙*/ img {border: 0; /*ie6*/vertical-align: middle; } /*讓button 按鈕 變成小手*/ button {cursor: pointer; } /*取消鏈接的下劃線*/ a {color: #666;text-decoration: none; }a:hover {color: #e33333; }button, input {font-family: 'Microsoft YaHei', 'Heiti SC', tahoma, arial, 'Hiragino Sans GB', \\5B8B\4F53, sans-serif;/*取消輪廓線 藍色的*/outline: none; }body {background-color: #fff;font: 12px/1.5 'Microsoft YaHei', 'Heiti SC', tahoma, arial, 'Hiragino Sans GB', \\5B8B\4F53, sans-serif;color: #666 }.hide, .none {display: none; } /*清除浮動*/ .clearfix:after {visibility: hidden;clear: both;display: block;content: ".";height: 0 }.clearfix {*zoom: 1 }

2car.css

.car-header {padding: 20px 0; }.car-logo img {vertical-align: middle; }.car-logo b {font-size: 20px;margin-top: 20px;margin-left: 10px; }.cart-filter-bar {font-size: 16px;color: #E2231A;font-weight: 700; }.cart-filter-bar em {padding: 5px;border-bottom: 1px solid #E2231A; }.cart-thead {height: 32px;line-height: 32px;margin: 5px 0 10px;padding: 5px 0;background: #f3f3f3;border: 1px solid #e9e9e9;border-top: 0;position: relative; }.cart-thead>div, .cart-item>div {float: left; }.t-checkbox, .p-checkbox {height: 18px;line-height: 18px;padding-top: 7px;width: 122px;padding-left: 11px; }.t-goods {width: 400px; }.t-price {width: 120px;padding-right: 40px;text-align: right; }.t-num {width: 150px;text-align: center; }.t-sum {width: 100px;text-align: right; }.t-action {width: 130px;text-align: right; }.cart-item {height: 160px;border-style: solid;border-width: 2px 1px 1px;border-color: #aaa #f1f1f1 #f1f1f1;background: #fff;padding-top: 14px;margin: 15px 0; }.check-cart-item {background: #fff4e8; }.p-checkbox {width: 50px; }.p-goods {margin-top: 8px;width: 565px; }.p-img {float: left;border: 1px solid #ccc;padding: 5px; }.p-msg {float: left;width: 210px;margin: 0 10px; }.p-price {width: 110px; }.quantity-form {width: 80px;height: 22px; }.p-num {width: 170px; }.decrement, .increment {float: left;border: 1px solid #cacbcb;height: 18px;line-height: 18px;padding: 1px 0;width: 16px;text-align: center;color: #666;margin: 0;background: #fff;margin-left: -1px; }.itxt {float: left;border: 1px solid #cacbcb;width: 42px;height: 18px;line-height: 18px;text-align: center;padding: 1px;margin: 0;margin-left: -1px;font-size: 12px;font-family: verdana;color: #333;-webkit-appearance: none; }.p-sum {font-weight: 700;width: 145px; }/* 結算模塊 */.cart-floatbar {height: 50px;border: 1px solid #f0f0f0;background: #fff;position: relative;margin-bottom: 50px;line-height: 50px; }.select-all {float: left;height: 18px;line-height: 18px;padding: 16px 0 16px 9px;white-space: nowrap; }.select-all input {vertical-align: middle;display: inline-block;margin-right: 5px; }.operation {float: left;width: 200px;margin-left: 40px; }.clear-all {font-weight: 700;margin: 0 20px; }.toolbar-right {float: right; }.amount-sum {float: left; }.amount-sum em {font-weight: 700;color: #E2231A;padding: 0 3px; }.price-sum {float: left;margin: 0 15px; }.price-sum em {font-size: 16px;color: #E2231A;font-weight: 700; }.btn-area {font-weight: 700;width: 94px;height: 52px;line-height: 52px;color: #fff;text-align: center;font-size: 18px;font-family: "Microsoft YaHei";background: #e54346;overflow: hidden; }

3common.css部分

/*公共樣式*/.fl {float: left; }.fr {float: right; }@font-face {font-family: 'icomoon';src: url('../fonts/icomoon.eot?7kkyc2');src: url('../fonts/icomoon.eot?7kkyc2#iefix') format('embedded-opentype'), url('../fonts/icomoon.ttf?7kkyc2') format('truetype'), url('../fonts/icomoon.woff?7kkyc2') format('woff'), url('../fonts/icomoon.svg?7kkyc2#icomoon') format('svg');font-weight: normal;font-style: normal; }.fr .icomoon {font-family: 'icomoon';font-size: 16px;line-height: 26px; }/*版心*/.w {width: 1200px;margin: 0 auto; }.style-red {color: #c81623; }.spacer {width: 1px;height: 12px;background-color: #666;margin: 9px 12px 0; }/*頂部快捷導航*/.shortcut {height: 31px;background-color: #f1f1f1;line-height: 31px; }.shortcut li {float: left; }/*header區域*/.header {position: relative;height: 105px; }.logo {position: absolute;top: 25px;left: 0;width: 175px;height: 56px; }.logo a {display: block;/*overflow: hidden;*/width: 175px;height: 56px;background: url(../img/logo.png) no-repeat;/*text-indent: -999px;*/font-size: 0; }.search {position: absolute;top: 25px;left: 348px; }.text {float: left;width: 445px;height: 32px;border: 2px solid #b1191a;padding-left: 10px;color: #ccc; }.btn {float: left;width: 82px;height: 36px;background-color: #b1191a;border: 0;font-size: 16px;color: #fff; }.hotwrods {position: absolute;top: 65px;left: 348px; }.hotwrods a {margin: 0 10px; }.shopcar {position: absolute;top: 25px;right: 64px;width: 138px;height: 34px;border: 1px solid #dfdfdf;background-color: #f7f7f7;line-height: 34px;text-align: center; }.car {font-family: 'icomoon';color: #da5555; }.arrow {font-family: 'icomoon';margin-left: 5px; }.count {position: absolute;top: -5px;/*應該是左側對齊 文字才能往右走顯示*/left: 100px;background-color: #e60012;height: 14px;padding: 0 3px;line-height: 14px;color: #fff;/*border-radius: 左上角 右上角 右下角 左下角;*/border-radius: 7px 7px 7px 0; }/*nav start*/.nav {height: 45px;border-bottom: 2px solid #b1191a; }.dropdown {width: 209px;height: 45px; }.dropdown .dt {height: 100%;background-color: #b1191a;font-size: 16px;color: #fff;text-align: center;line-height: 45px; }.dropdown .dd {height: 465px;background-color: #c81623;margin-top: 2px; }.menu_item:hover {background-color: #fff; }/*鼠標經過li 里面的 a變顏色*/.menu_item:hover a {color: #c81623; }.menu_item {height: 31px;line-height: 31px;margin-left: 1px;padding: 0 10px;transition: all .5s; }.menu_item:hover {padding-left: 20px; }.menu_item a {font-size: 14px;color: #fff; }.menu_item i {float: right;font-family: 'icomoon';font-size: 18px;color: #fff; }.navitems {margin-left: 10px; }.navitems li {float: left; }.navitems li a {display: block;height: 45px;padding: 0 25px;line-height: 45px;font-size: 16px; }/*footer 部分*/.footer {height: 386px;background-color: #f5f5f5;padding-top: 30px; }.mod_service {height: 79px;border-bottom: 1px solid #ccc; }.mod_service li {float: left;width: 240px;height: 79px; }.mod-service-icon {/*浮動的盒子 可以直接給大小的 不需要轉換*/float: left;width: 50px;height: 50px;margin-left: 35px;background: url(../img/icons.png) no-repeat; }.mod_service_zheng {background-position: -253px -3px; }.mod_service_tit {float: left;margin-left: 5px; }.mod_service_tit h5 {margin: 5px 0; }.mod_service_kuai {background-position: -255px -54px; }.mod_service_bao {background-position: -257px -105px; }.mod_help {height: 187px;border-bottom: 1px solid #ccc; }.mod_help_item {float: left;width: 150px;padding: 20px 0 0 50px; }.mod_help_item dt {height: 25px;font-size: 16px; }.mod_help_item dd {height: 22px; }.mod_help_app dt, .mod_help_app p {padding-left: 15px; }.mod_help_app img {margin: 7px 0; }.mod_copyright {text-align: center; }.mod_copyright_links {margin: 20px 0 15px 0; }.mod_copyright_info {line-height: 18px; }

總結

以上是生活随笔為你收集整理的前端经典案例——购物车,Jquery实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。