Blogger Information
Blog 10
fans 1
comment 0
visits 6565
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
购物车案例_作业0122
红色熊猫的博客
Original
680 people have browsed it

实例

<!DOCTYPE HTML>
<html>
<head>
<title>点击商品选中效果</title>
<meta charset="utf-8"/>
 <link rel="icon" type="image/x-icon" href="images/2.png"> 
 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
 <style type="text/css">
	* {margin: 0px auto;padding: 0px;}
	.top {width: 402px;height: 35px;
		  line-height: 35px;text-align:center;
		  margin-top: 50px;
		  background: #C40000;color:#fff;
		}
	
    .main {width: 400px;height: 400px;border: 1px solid #C40000;}
	p {width: 400px;height: 26px;margin-top:10px;}
	b {width: 90px;height: 26px;
	   line-height: 26px;text-align: center;
	   font-size: 12px;color:#838383;
	   border: 1px solid #ccc;   
	   float: left;margin-left: 5px;}
	span {width: 90px;height: 26px;
		line-height: 26px;text-align: center;
		font-size: 12px;color:#838383;
		border: 1px solid #ccc;
		float: left;margin-left: 5px;
		display: block;}
	span:hover {cursor: pointer;}
	button {width: 120px;height: 35px;background: #C40000;color: white;border: 0px;}
	button:hover {cursor: pointer;}
	.notice{border:0px;}
   
	.select {width: 88px;height: 24px;line-height: 24px;border: 2px solid #ff0000;color: red}
	 
</style>

</head>
<body>
  <div class="top">请选择信息后加入购物车</div>
	<div class="main">
		<p class="item" name="version">
			<b class="notice">版本</b>
			<span>ONE A2001</span>
			<span>ONE A0001</span>
			<span>ONE A1001</span>
		</p>
		<p class="item" name="color">
			<b class="notice">机身颜色</b>
			<span>白色</span>
			<span>黑色</span>
			<span>金色</span>
		</p>
		<p class="item" name="type">
			<b class="notice">套餐类型</b>
			<span>标配</span>
			<span>套餐一</span>
			<span>套餐二</span>
		</p>
		<p class="item" name="ram">
			<b class="notice">运行内存</b>
			<span>2GB</span>
			<span>3GB</span>
			<span>4GB</span>
		</p>
		<p class="item" name="rom">
			<b class="notice">机身内存</b>
			<span>16GB</span>
			<span>32GB</span>
			<span>64GB</span>
		</p>
		<p class="item" name="location">
			<b class="notice">产地</b>
			<span>中国大陆</span>
			<span>港澳台</span>
		</p>
		<p class="item" name="price">
			<b class="notice">价格</b>
			<span>999元抢购</span>
		</p>
		<p class="item1" name="num">
			<b class="notice">数量</b>
			<input type="number" value="1" style="width:40px;height:26px;">
		</p>

		<p style="margin-top:30px;margin-left:95px;">
			<button class="bu1" id='sub'>加入购物车</button>
		</p>		
  </div>
<script>

$(function(){
	 
	$('span').click(function(){
		$(this).addClass('select');
		$(this).siblings().removeClass('select');//同级的select类全部去掉
	})

	$('#sub').click(function(){
		var obj={};//创建新空对象 存放商品属性值
		
		$('.item').each(function(){
			//获取当前p标签下的select选项状态,hasClass表示被选元素是否含有;children(name) 筛选子元素
			// var len=$(this).children('span').hasClass('select').length;
			// console.log(len);

			var len=$(this).children('span.select').length;//span.select不留空格表示span标签含有class=select属性

			if(len<=0){
				var t=$(this).children('b').html()+'未选中';
				alert(t);
				return false;
			}else{
				var key=$(this).attr('name');//获取当前p标签的name的属性值
				var value=$(this).children('span.select').html();//获取被选中的商品值
				obj[key]=value;//将键值一一对应
			}
			
			// 判断数量
			var n=$('.item1').children('input').val();
			if(parseInt(n)<=0){
				alert('填写正确的数量');
				return false;
			}else{
				obj['num']=n;
			}

		})
		
		console.log(obj);
	})
	
})


</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post