购物车初级代码

        这是一个非常非常非常初级的购物车代码。其中的总计,批量购买 按钮没有实现,并且删除按钮会出现bug。

        下面是代码:

<html>
	<head>
		<title>JS初识(3)</title>
		<script>
		
			function buy_produced(produced_name_id,produced_price_id){
				//获得商品的复选框状态
				var choose_result=document.getElementById(produced_name_id+'_choose');
				//先获得商品的名称和价格
				if(choose_result.checked){
				
				var produced_name =document.getElementById(produced_name_id);
				var produced_price=document.getElementById(produced_price_id);
				var produced_name_text=produced_name.innerText;
				var produced_price_text=produced_price.innerText;
				//得到描述购物车的表并的得到其中的HTML元素
				var table_name=document.getElementById('shopping_car');
				var table_str=table_name.innerHTML;
				//商品数量
				var produced_count=1;
				//拼接列表,用节点的方式
				var get_result=table_str.toString().search(produced_name_text);
				if(get_result == -1){
					//说明表中没有该商品
					//创建tr和td和text内容
					var new_tr=document.createElement('tr');
					var new_td=document.createElement('td');
					var new_td_text=document.createTextNode(produced_name_text);
					new_td.setAttribute('id',produced_name_id);
					var new_td2=document.createElement('td');
					var new_td_text2=document.createTextNode(produced_price_text);
					new_td2.setAttribute('id',produced_price_id);
					var new_td3=document.createElement('td');
					var new_td_text3=document.createTextNode(produced_count);
					new_td3.setAttribute('id',produced_name_id+'_count');
					new_td3.setAttribute('align','center');
					var new_td4=document.createElement('td');
					var new_td_text4=document.createTextNode('故障');
					new_td4.setAttribute('id',produced_name_id+'_zongji');
					var new_td5=document.createElement('td');
	//				var new_td_text5=document.createHTMLNode('<input type="button" value="删除">');
					new_td5.innerHTML="<input type='button' value='删除' onclick=delete_producted(produced_name_id)/>";
					new_td5.setAttribute('id',produced_name_id+'_delete');
					new_td5.setAttribute('align','center');
					//将tr,td和table进行拼接
					new_td.appendChild(new_td_text);
					new_td2.appendChild(new_td_text2);
					new_td3.appendChild(new_td_text3);
					new_td4.appendChild(new_td_text4);
	//				new_td5.appendChild(new_td_text5);
					new_tr.appendChild(new_td);
					new_tr.appendChild(new_td2);
					new_tr.appendChild(new_td3);
					new_tr.appendChild(new_td4);
					new_tr.appendChild(new_td5);
					table_name.appendChild(new_tr);
					//不要用这种传统的字符串拼接形式。   var table_str=table_name.innerHTML;
				}
			
			
					else{
					//说明购物车中有商品,只需要替换 产品数量的列即可
					//得到要被替换的列节点
					var old_node=document.getElementById(produced_name_id+'_count');
					var old_node_number=parseInt(old_node.innerText);
					old_node_number += 1;
					//创建新的列节点
					var new_td4=document.createElement('td');
					var new_td_text4=document.createTextNode(old_node_number.toString());
					new_td4.setAttribute('id',produced_name_id+'_count');
					new_td4.setAttribute('align','center');
					new_td4.appendChild(new_td_text4);
					//新节点替换旧节点
					old_node.parentNode.replaceChild(new_td4,old_node);
					
					}
				}
				else{
					alert('请选择商品');
				}
			}
			
			function reduce(produced_name_id){
				//判断复选框的状态
				var choose_result=document.getElementById(produced_name_id+'_choose');
				if(choose_result.checked){
				//判断是否有这个商品
				//先获得商品的名称和价格
				var table_name=document.getElementById('shopping_car');//得到表引用
				var table_str=table_name.innerHTML;
				var produced_exit=table_str.toString().search(produced_name_id);
				if(-1==produced_exit){
					alert('商品不存在');
				}
				//判断该商品在购物车中是否存在
				else {
					//说明没有该商品
					var produced_count =document.getElementById(produced_name_id+'_count');
					var produced_count_text=produced_count.innerText;
					var result=parseInt(produced_count_text);
					if(0==result){
					alert('商品的数量已经为0');
					}
					else{
				
					//说明有该商品,数量要减少一 produced_name_id+'_count'
					//得到原来的单元格
					var old_Node = document.getElementById(produced_name_id+'_count');
					var old_Node_text=parseInt(old_Node.innerText);
					//创建新的单元格
					var new_Node=document.createElement('td');
					var new_Node_text=document.createTextNode(old_Node_text-1);
					new_Node.setAttribute('id',produced_name_id+'_count');
					new_Node.setAttribute('align','center');
					new_Node.appendChild(new_Node_text);
					//替换单元格
					old_Node.parentNode.replaceChild(new_Node,old_Node);
				}	
					
			}
		}	
					else{
						alert('请选择商品');
					}
	}
			function delete_producted(produced_name_id){
				var msg='删除这条数据';
				if(confirm(msg)==true){
				var shopping_car=document.getElementById('shopping_car');//得到表引用
				//获取商品所在的tr元素
				var producted_row=document.getElementById(produced_name_id+'_row');
				shopping_car.delete(producted_row);
				}
				else{
					return;
				}
			}
			
			function choose_all(){
				//先得到全选框的标签
				var result=document.getElementById('choose_all');
				//判断复选框的状态
				if(result.checked){
					//当复选框被勾选的时候,判断复选框的状态为true
					//得到其他复选框的引用
					var other_produced_list = document.getElementsByClassName('produced');
					for(var produced in other_produced_list){
						other_produced_list[produced].checked=true;
					}
				}
				else{
					var other_produced_list = document.getElementsByClassName('produced');
					for(var produced in other_produced_list){
						other_produced_list[produced].checked=false;
					}
				}
			}
		</script>
	</head>
	<body>
		<center>
		<!--创建商品表-->
		<table style="border:1px solid #9999FF;background:#33FFFF">
			<caption><b>商品表</b></caption>
			<tr>
				<th>
					全选<input  id ="choose_all" type="checkbox" onclick="choose_all()"/>
				</th>
				<th>商品名</th>
				<th>价格(单价)</th>
				<th>调节数量</th>
				<th>操作</th>
			</tr>
			<tr id="yingyangkuangxian_row">
				<td>
					<input id="yingyangkuangxian_choose" class="produced" type="checkbox"/>
				</td>
				<td id="yingyangkuangxian">
					营养快线
				</td>
				<td id="yingyangkuangxianPrice">
					3元
				</td>
				<td>
				<input type="button" value="-" onclick="reduce('yingyangkuangxian')"/>
					1
				<input type="button" value="+" onclick="buy_produced('yingyangkuangxian','yingyangkuangxianPrice')"/>
				</td>
				<td>
					<input type="button" value="购买" onclick="buy_produced('yingyangkuangxian','yingyangkuangxianPrice')"/>
				</td>
			</tr>
			<tr id="anmuxi_row">
				<td>
					<input id="anmuxi_choose" class="produced" type="checkbox"/>
				</td>
				<td id="anmuxi">
					安慕希
				</td>
				<td id="anmuxiPrice">
					8元
				</td>
				<td>
				<input type="button" value="-" onclick="reduce('anmuxi')"/>
					1
				<input type="button" value="+" onclick="buy_produced('anmuxi','anmuxiPrice')"/>
				</td>
				<td>
					<input type="button" value="购买" onclick="buy_produced('anmuxi','anmuxiPrice')"/>
				</td>
				
			</tr>
			<tr id="meinianda_row">
				<td>
					<input  id="meinianda_choose" class="produced" type="checkbox"/>
				</td>
				<td id="meinianda">
					美年达
				</td>
				<td id="meiniandaPrice">
					3元
				</td>
				<td>
				<input type="button" value="-" onclick="reduce('meinianda')"/>
					1
				<input type="button" value="+" onclick="buy_produced('meinianda','meiniandaPrice')"/>
				</td>
				<td>
					<input type="button" value="购买" onclick="buy_produced('meinianda','meiniandaPrice')"/>
				</td>
			</tr>
			<tr>
				<td colspan="4">
					<input type="button" value="批量购买"/>
				</td>
			</tr>
		</table>
		<br/><br/><br/><br/>
		<table id="shopping_car" style="border:1px solid #9999FF;background:#33FFFF;align:center">
			<caption><b>购物车</b></caption>
			<tr>
				<th>商品名</th>
				<th>价格</th>
				<th>数量</th>
				<th>总计</th>
				<th>操作</th>
			</tr>
		</table>
		</center>
	</body>
</html>

         页面显示为:

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
js加入购物车抛物线动画与购物车效果特效,亲测可用, 当您在电商购物网站浏览中意的商品时,您可以点击页面中的“加入购物车”按钮即可将商品加入的购物车中。本文介绍借助一款基于jQuery的动画插件,点击加入购物车按钮时,实现商品将飞入到右侧的购物车中的效果。 HTML 首先载入jQuery库文件和jquery.fly.min.js插件。 复制代码 代码如下: 接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品box中包括有商品图片、价格、名称以及加入购物车按钮等信息。 复制代码 代码如下: ¥3499.00 LG 49LF5400-CA 49寸IPS硬屏富贵招财铜钱设计 加入购物车 ¥3799.00 Hisense/海信 LED50T1A 海信电视官方旗舰店 加入购物车 ¥¥3999.00 Skyworth/创维 50E8EUS 8核4Kj极清酷开系统智能液晶电视 加入购物车 ¥6969.00 乐视TV Letv X60S 4核1080P高清3D安卓智能超级电视 加入购物车 然后,我们还需要在页面的右侧加上购物车以及提示信息。 复制代码 代码如下: 购物车 已成功加入购物车! CSS 我们使用CSS先将商品排列美化,然后设置右侧购物车样式,具体请看代码: 复制代码 代码如下: .box{float:left; width:198px; height:320px; margin-left:5px; border:1px solid #e0e0e0; text-align:center} .box p{line-height:20px; padding:4px 4px 10px 4px; text-align:left} .box:hover{border:1px solid #f90} .box h4{line-height:32px; font-size:14px; color:#f30;font-weight:500} .box h4 span{font-size:20px} .u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;} .m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 35px;height: 100%;font-size: 12px;color: #fff;} .cart{color: #fff;t
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值