JS模拟百度音乐-全选操作

CSS部分:

body,ul{margin:0; padding:0;}
	ul{margin:100px auto 0; width:200px;font-size:12px; background:white;}
	li{list-style:none; height:30px; line-height:30px; border-bottom:1px solid #fff;}
	span{float:right;}
	#bottom{width:200px; margin:0px auto;font-size:12px; height:30px;}
	#bottom input{float:left;}
	#bottom a{width:30px; line-height:20px; text-decoration:none; text-align:center; float:right; margin-left:5px; color:#999; border:1px solid #999; border-radius:2px;}
	.disabled{cursor:no-drop;}

HTML部分:

<ul>
	<li><input type="checkbox" /> 私奔<span>梁博</span></li>
	<li><input type="checkbox" /> 北京北京<span>汪峰</span></li>
	<li><input type="checkbox" /> 过火<span>张信哲</span></li>
	<li><input type="checkbox" /> 空城<span>杨坤</span></li>
	<li><input type="checkbox" /> 我是一匹来自北方的狼<span>齐秦</span></li>
	<li><input type="checkbox" /> 白天不懂夜的黑<span>那英</span></li>
</ul>
<div id="bottom">
	<input type="checkbox"/> 全选
	<a href="http://www.baidu.com">删除</a>
	<a href="http://www.baidu.com">添加</a>
	<a href="http://www.baidu.com">收藏</a>
</div>

JS部分:

window.onload = function(){
		var aLi = document.getElementsByTagName('li');
		var aInput = document.getElementsByTagName('input');
		var oDivBottom = document.getElementById('bottom');
		var aA = oDivBottom.getElementsByTagName('a');
		var arrColor = ['#fff','#cff'];
		var oParLi = null;
		var checkedInputNum = 0;

		// 设置底部a链接状态
		setAclassName();
		function setAclassName(){
			// 循环遍历所有a
			for(var i = 0; i < aA.length; i++){
				// 判断选中的复选框个数
				if(checkedInputNum > 0){
					aA[i].className = '';
				}else{
					aA[i].className = 'disabled';
				}
			}
		};

		// 循环遍历所有的li
		for (var i = 0; i < aLi.length; i++) {
			aLi[i].index = i;
			// 隔行变色
			aLi[i].style.background = arrColor[i%arrColor.length];

			// 当鼠标经过li时,让当前的li变成灰色
			aLi[i].onmouseover = function(){
				this.style.backgroundColor = '#eee';
			};

			// 当鼠标移开时li的背景色状态变回之前的状态
			aLi[i].onmouseout = function(){
				// 如果当前li所在的input没有被选中
				if(!aInput[this.index].checked){
					this.style.backgroundColor = arrColor[this.index%arrColor.length];
				}
			};

			// 当点击input复选框
			aInput[i].onclick = function(){
				// 获取当前复选框的父元素li
				oParLi = this.parentNode;
				// 如果当前input是选中状态
				if(this.checked){
					// 改变背景色#eee,同时获取选中的数量
					oParLi.style.backgroundColor = '#eee';
					checkedInputNum ++;
					// console.log(checkedInputNum);
				}else{
					checkedInputNum --;
					// console.log(checkedInputNum);
				}
				setAclassName();

				// 判断如果复选框的数量等于它的长度-1[不包含最后一个]
				if(checkedInputNum == aInput.length - 1){
					// 就让最后一个选中
					aInput[aInput.length - 1].checked = true;
				}else{
					// 否则就不选中
					aInput[aInput.length - 1].checked = false;
				}
			};

			// 当点击最后一个复选框input时
			aInput[aInput.length - 1].onclick = function(){
				for (var i = 0; i < aLi.length; i++) {
					// 如果当前input是选中状态
					if(this.checked){
						// 让所有遍历的input复选框选中,并设置背景色和获取选中数量
						aInput[i].checked = true;
						aLi[i].style.backgroundColor = '#eee';
						checkedInputNum = aInput.length - 1;
					}else{
						// 否则就让所有遍历的input复选框不选中,设置背景色和重置选中数量为0
						aInput[i].checked = false;
						aLi[i].style.backgroundColor = arrColor[i%arrColor.length];
						checkedInputNum = 0;
					}
				}
				setAclassName();
			};
		};

	};

预览效果:




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值