本文主要介绍了基于JQuery的购物车添加删除以及结算功能示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能帮助到大家。
前段时间了解到购物车结算算是一个难点部分,在网上也找了一些,但是网上除了插件之外,就是一些半成品,比如一部分只有添加删除效果,另一部分只有结算功能,很少见到整合在一起的购物车效果,因此自己写了一个,方便大家查看
(添加效果没有飞入,实在懒得写动画效果了,凑合看吧)
HTML部分
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <link rel="stylesheet" href="css/index.css" rel="external nofollow" > <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script> <script src="js/index.js" type="text/javascript" charset="utf-8" async defer></script> </head> <body> <p id="banner"></p> <p id="container"> <ul> <li num="1"><img src="images/1.jpg"/><span class="things_name">2014年春季爆品A<p>$<i>10</i><span class="buy">点击购买</p></li> <li num="2"><img src="images/2.jpg"/><span class="things_name">2014年春季爆品B<p>$<i>20</i><span class="buy">点击购买</p></li> <li num="3"><img src="images/3.jpg"/><span class="things_name">2014年春季爆品C<p>$<i>30</i><span class="buy">点击购买</p></li> <li num="4"><img src="images/4.jpg"/><span class="things_name">2014年春季爆品D<p>$<i>40</i><span class="buy">点击购买</p></li> </ul> </p> <p id="carlist"> <p class="car"> <p> <span class="carLogo"><span><img src="images/car.png"/> <span class="txt">购<br />物<br />车 </p> </p> <p class="list"> <!--此处添加动态元素--> <p class="total"> <span>总价:<span>0元 </p> </p> </p> <script type="text/javascript"> function view(){ return{ w:document.documentElement.clientWidth, h:document.documentElement.clientHeight }; } document.body.style.height=view().h+"px"; </script> </body> </html>
JS部分
$(function(){ var mark=0; $(".car").on("click",function(){ if(mark==0){ $("#carlist").animate({marginRight:"0px"},500) mark=1; }else{ $("#carlist").animate({marginRight:"-260px"},500) mark=0 } }) //点击购买按钮添加至购物车 var buyButton=$(".buy"); buyButton.on("click",BuyClick) function BuyClick(){ var thingsName=$(this).parents("li").find(".things_name").text(); var thingsPrice=$(this).parent().find("i").text(); var thingsImage=$(this).parents("li").find("img").attr("src"); var kNum=$(this).parents("li").attr("num") var Geshu=1; $(this).off("click").text("已经添加至购物车"); $(".list").append('<p class="select things" num='+kNum+'><img src='+thingsImage+'/><p class="name">'+thingsName+'</p><p class="price">$<i>'+thingsPrice+'</i></p><ul class="caozuo"><li class="zengjian"><span class="minus">-<span>1<span class="add">+</li><li class="del">删除</li></ul></p>'); countTotalPrice(); totalGeshu(); //点击加号添加商品个数 $(".add").off("click").on("click",function(){ Geshu=parseInt($(this).parent().find("span:nth-of-type(2)").text()) Geshu++ $(this).parent().find("span:nth-of-type(2)").text(Geshu) countTotalPrice(); totalGeshu(); }) //动态生成的元素点击减号减少商品个数 $(".minus").off("click").on("click",function(){ Geshu=parseInt($(this).parent().find("span:nth-of-type(2)").text()); if(Geshu>1){ Geshu--; $(this).parent().find("span:nth-of-type(2)").text(Geshu) }else{ Geshu==1; } countTotalPrice(); totalGeshu(); }) //删除购物车内的商品 var del=$(".del"); del.each(function(){ $(this).off("click").on("click",function(){ var delName=$(this).parents(".things").find(".name").text(); $(this).parents(".things").remove(); countTotalPrice(); totalGeshu(); var oldBtn=$("#container ul li").find("span:contains("+delName+")").parents("li").find(".buy") oldBtn.on("click",BuyClick).text("点击购买") }) }) //计算总价函数 function countTotalPrice(){ var totalPrice=0,listThings=$(".list").find(".things"); for (var i=0;i<listThings.length;i++) { var this_geshu=parseInt(listThings.eq(i).find(".zengjian span:nth-of-type(2)").text()); var this_price=parseInt(listThings.eq(i).find(".price i").text()); totalPrice+=this_geshu*this_price; } $(".total span").eq(1).text(totalPrice); totalGeshu(); } //购物车上的商品总数 function totalGeshu(){ var listThings=$(".list").find(".things"); if (listThings.length>0) { var totalGeshu=0; listThings.each(function(){ var this_geshu=parseInt($(this).find(".zengjian span:nth-of-type(2)").text()); totalGeshu+=this_geshu; }) $(".carLogo span").html(totalGeshu) } else{ $(".carLogo span").css("display","none") } } } })
相关推荐:
以上是JQuery实现购物车添加删除以及结算功能的详细内容。更多信息请关注PHP中文网其他相关文章!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版
好用的JavaScript开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Atom编辑器mac版下载
最流行的的开源编辑器