Javascript实现购物车案例--Javascript学习

JavaScript实现购物车来进行设计一个购物车的功能

html代码:



	
		
		购物车
		
		
	
	
		
全选 商品 单价 数量 小计 操作
Sony/索尼 DSC-WX300 1999 - + 1999 删除
Casio/卡西欧 EX-TR350 2999 - + 2999 删除
Canon/佳能 PowerShot SX50 HS 3999 - + 3999 删除
Fujifilm/富士 instax mini 25 4999 - + 4999 删除

css代码:

@charset "utf-8";

* {
	margin: 0px;
	padding: 0px;
}

body {
	color: #666;
	font: 12px/180% Arial, Helvetica, sans-serif, "新宋体";
}

table {
	border-collapse: collapse;
	/**/
	border-spacing: 0;
	border: 0px;
}

a {
	color: #666;
	text-decoration: none;
}

/* 主体 */
/*浮动*/
.fl {
	float: left;
}

.fr {
	float: right;
}

/* 清除浮动 */
.clearfix:after {
	content: "";
	display: block;
	height: 0px;
	clear: :both;
	visibility: hidden;
}

.clearfix {
	display: inline-block;
}

/* 清除浮动完成 */
.shoppingCar {
	width: 940px;
	margin: 100px auto;
	/* position: absolute; */
}

.shoppingCar table {
	text-align: center;
	width: 100%;
}

.shoppingCar table th,
.shoppingCar table td {
	border: 1px solid darkgray;
}

.shoppingCar table th {
	background-color: aqua;
	border-top: 3px solid darkgray;
	height: 30px;
}

.shoppingCar table td {
	padding: 10px;
	color: #444;
}

.shoppingCar table tbody tr:hover {
	background: rgba(238, 246, 255);
}

.checkbox {
	width: 60px;
	/*定义复选框宽度*/
}

.check_all {
	vertical-align: middle;
	/*让复选框居中*/
}

.goods {
	width: 300px;
}

.goods span {
	width: 180px;
	margin-top: 20px;
	text-align: left;
	float: left;
}

.goods img {
	width: 100px;
	height: 80px;
	margin-right: 10px;
	float: left;
}

.price {
	width: 130px;
}

.count {
	width: 90px;
}

.count input,
.count .add,
.count.reduce {
	float: left;
	margin-right: -1px;
	position: relative;
	z-index: 0;
}

.count .add,
.count .reduce {
	height: 23px;
	width: 17px;
	border: 1px solid darkgray;
	background-color: white;
	text-align: center;
	line-height: 23px;
	color: #444444;
}

/* .count .add{
	float: right;
} */
/* .conunt.reduce{
	float:left;
} */

.count .add:hover,
.count.reduce:hover {
	color: #F50;
	z-index: 3;
	border-color: #F60;
	cursor: pointer;
	/*设置鼠标点击的样式*/
}

.count input {
	width: 50px;
	height: 15px;
	line-height: 15px;
	border: 1px solid #aaa;
	color: #343;
	text-align: center;
	padding: 4px 0;
	background-color: #fff;
	z-index: 2;
}

.xj {
	width: 150px;
	color: red;
	font-weight: 700;
}

.operation span:hover,
a:hover {
	cursor: pointer;
	color: red;
	text-decoration: underline;
}

.foot {
	margin-top: 0px;
	color: #666;
	height: 48px;
	border: 1px solid #c8c8c8;
	border-top: 0;
	background-color: #eaeaea;
	background-image: linear-gradient(RGB(241, 241, 241), RGB(226, 226, 226));
	position: relative;
	z-index: 8;
}

.foot div,
.foot a {
	line-height: 48px;
	height: 48px;
}

.foot .select_all {
	width: 80px;
	height: 48px;
	line-height: 48px;
	text-align: center;
	color: #666;
}

.foot .delete {
	padding-left: 10px;
}

.foot .closing {
	border-left: 1px solid #C8C8C8;
	/* border-top: ; */
	width: 103px;
	text-align: center;
	color: #666;
	font-weight: bold;
	cursor: pointer;
	background-image: linear-gradient(RGB(241, 241, 241), RGB(226, 226, 226));
}

.foot .closing:hover {
	background-image: linear-gradient(RGB(226, 226, 226), RGB(241, 241, 241));
	/*线性渐变图像*/
	color: #333;
}

.foot .total {
	margin: 0 20px;
	cursor: pointer;
}

.foot #priceTotal,
.foot #selectedTotal {
	color: red;
	font-family: "Microsoft Yahei";
	font-weight: bold;
}

.foot .selected {
	cursor: pointer;
}

.foot .selected .arrow {
	position: relative;
	top: -3px;
	margin-left: 3px;
}

.foot .selected .down {
	position: relative;
	top: 3px;
	display: none;
}

.show .selected .down {
	display: inline;
}

.show .selected .up {
	display: none;
}

.foot .selected:hover .arrow {
	color: red;
}

.foot .selected-view {
	width: 938px;
	border: 1px solid #c8c8c8;
	position: absolute;
	height: auto;
	background: #ffffff;
	z-index: 9;
	bottom: 48px;
	left: -1px;
	display: none;
}

.show .selected-view {
	display: block;
}

.foot .selected-view div {
	height: auto;
}

.foot .selected-view .arrow {
	font-size: 16px;
	line-height: 100%;
	color: #c8c8c8;
	position: absolute;
	right: 330px;
	bottom: -9px;
}

.foot .selected-view .arrow span {
	color: #ffffff;
	position: absolute;
	left: 0px;
	bottom: 1px;
}

#selectedViewList {
	padding: 10px 20px 10px 20px;
}

#selectedViewList div {
	display: inline-block;
	position: relative;
	width: 100px;
	height: 80px;
	border: 1px solid #ccc;
	margin: 10px;
	float: left;
}

#selectedViewList div img {
	width: 100px;
	height: 80px;
	margin-right: 10px;
	float: left;
}

#selectedViewList div span {
	display: none;
	color: #ffffff;
	font-size: 12px;
	position: absolute;
	top: 0px;
	right: 0px;
	width: 60px;
	height: 18px;
	line-height: 18px;
	text-align: center;
	background: #000;
	cursor: pointer;
}

#selectedViewList div:hover span {
	display: block;
}

js代码:

window.onload=function(){
	
	var table=document.getElementById('tableShop');//购物车表格
	var selectInput=document.getElementsByClassName("check");//所有单选框
	var checkAllInput=document.getElementsByClassName("check_all");//所有全选框
	var tr=table.children[1].rows;//获取表格第n行
	var selectToTal=document.getElementById("selectedTotal");//已选商品数目容器
	var priceToTal=document.getElementById("priceTotal");//合计的价格
	var deleteAll=document.getElementById("deleteAll");//删除全部
	var foot=document.getElementById("foot");//获取子列表
	var selected=document.getElementById("selected");
	var selectedViewList=document.getElementById("selectViewList");
	console.log(foot);
function getTotal() {
		var seleted = 0;
		var price = 0;//
		var HTMLstr = "";
		for (var i = 0;i取消选择
' } else { tr[i].className = ''; } } selectToTal.innerHTML = seleted; priceToTal.innerHTML = price.toFixed(2); selectedViewList.innerHTML = HTMLstr; if (seleted == 0) { foot.className = 'foot'; } } //计算单行价格 function getSubToTal(){ for(var i=0;i=0){ for(var j=0;j1){ countInout.value=value-1; getSubToTal(this); }break; case "delete": var con=confirm("确定删除此商品嘛"); if(con){ this.parentNode.removeChild(this); } break; } getTotal(); } //显示商品弹层 selected.onclick = function () { console.log("测试"); if (selectedTotal.innerHTML != 0) { foot.className = (foot.className == 'foot' ? 'foot show' : 'foot'); } } // 取消选择的按钮 selectedViewList.onclick = function (e) { var e = e || window.event; var el = e.srcElement; if (el.className=='del') { var input = tr[el.getAttribute('index')].getElementsByTagName('input')[0] input.checked = false; input.onclick(); } } tr[i].getElementsByTagName("input")[1].onkeyup=function(){ var val=parseInt(this.value); if(isNaN(val)||val<=0){ val=1; } if(this.value!=val){ this.value=val; getSubToTal(this.parentNode.parentNode); //更新小计 getTotal(); //更新总数 } } //删除全部 deleteAll.onclick=function(){ console.log("123"); if(selectToTal.innerHTML!=0){ var con=confirm("确定删除所选商品嘛"); if(con){ for(var i=0;i

 

你可能感兴趣的:(#,javascript)