<style type="text/css">
/*清除樣式*/
*{margin: 0;padding: 0;}
ul li{list-style-type: none;}
a{text-decoration: none; color: inherit;}
/*---------------------------------------------------*/
.wrap{
width: 600px;
margin: 100px auto;
font-size: 14px;
}
.wrap ul li{
width: 100%;
height: 35px;
margin-bottom: 1px;
background: yellow;
line-height: 35px;
text-indent: 10px;
}
.wrap ul li a{
display: inline-block;
width: 20px;
height: 20px;
margin-right: 5px;
border: 1px solid white;
background: url('images/ck.png') no-repeat center/cover;
vertical-align: -4px;
}
.wrap ul li:nth-child(3n-1){
background: #ff0000;
}
.wrap ul li:nth-child(3n){
background: green;
}
.wrap .ckbox{
margin-top: 15px;
padding-left: 10px;
}
.wrap .ckbox:after{
content: '';
display: block;
clear: both;
}
.wrap .ckbox span{
float: left;
line-height: 20px;
cursor: pointer;
}
.wrap .ckbox span:first-child{
width: 60px;
height: 20px;
margin-right: 10px;
background: url('images/ck.png') no-repeat;
text-indent: 25px;
}
.wrap .ckbox span.on,
.wrap ul li a.on{
background-image: url('images/cked.jpg');
}
</style>
</head>
<body>
<div class="wrap">
<ul>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
<li><a href="javascript:;"></a>你好你好</li>
</ul>
<div class="ckbox">
<span>全選</span>
<span>反選</span>
</div>
</div>
<script type="text/javascript">
var oWrap = document.getElementsByClassName( 'wrap' )[0],
aBtn = oWrap.getElementsByTagName( 'a' ),
aCk = oWrap.getElementsByTagName( 'span' ),
bool = true,
length = aBtn.length,
num = 0;
for(var i=0 ; i<length ; i++){
aBtn[i].onclick = function (){
var tList = this.classList;
if( tList.contains( 'on' ) ){
tList.remove( 'on' );
num --;
} else {
tList.add( 'on' );
num ++ ;
}
aCk[0].classList[ num=== length ? 'add' : 'remove']( 'on' );
}
}
//全選
aCk[0].onclick = function (){
var tList = this.classList;
tList.toggle( 'on' );
if( tList.contains('on') ){
for( i=0 ; i<length ; i++ ){
aBtn[i].classList.add( 'on' );
num = 12;
}
} else {
for( i=0 ; i<length ; i++ ){
aBtn[i].classList.remove( 'on' );
num = 0;
}
}
}
//反選
aCk[1].onclick = function (){
num = length - num;
for(i=0 ; i<length ; i++ ){
// aBtn[i].classList[ aBtn[i].classList.contains( 'on' ) ? 'remove' : 'add']( 'on' );
var aList = aBtn[i].classList;
aList[ aList.contains( 'on' ) ? 'remove' : 'add']( 'on' );
}
aCk[0].classList[ num=== length ? 'add' : 'remove']( 'on' );
}
//點擊選中
// for(var i=0 ; i<length ; i++){
// aBtn[i].onclick = function (){
// var tcn = this.className;
// if( tcn === 'on'){
// this.className = '';
// num -- ;
// } else{
// this.className = 'on';
// num ++ ;
// }
// aCk[0].className = num === length ? 'on' : '';
// }
// }
// //全選
// aCk[0].onclick = function (){
// if( this.className === ''){
// this.className = 'on';
// for( i=0 ; i<length ; i++){
// if( aBtn[i].className === '' ){
// aBtn[i].className = 'on';
// num = 12;
// }
// }
// } else{
// this.className = '';
// for( i=0 ; i<length ; i++){
// aBtn[i].className = '';
// num = 0;
// }
// }
// }
// //反選
// aCk[1].onclick = function (){
// num = length - num;
// for(i=0 ; i<length ; i++ ){
// aBtn[i].className = aBtn[i].className === '' ? 'on' : '';
// }
// aCk[0].className = num === length ? 'on' : '';
// }
</script>
知識點
classList:返回class里的所有值:類數(shù)組竹捉; (不兼容IE8及以下)
classList.add( ' ' ) 加入,加入某個類名;
classList.remove( ' ' ) 移除簸喂,移除某個類名毙死;
classList.toggle( ' ' ) 切換類名(有則刪,沒有則加)
例:
<div class = 'a b c d'></div>
var aDiv = document.getElementsByTagName( 'div' )[0];
console.log( aDiv.classList );
彈出:a b c d
移除a b
aDiv.classList.remove( 'a' , 'b' );
移除a
aDiv.classList.remove( 'a' );
加入
aDiv.classList.add( 'a' );
contains() 判斷是否有某個類名喻鳄,返回一個布爾值
<div class = 'a b c d'></div>
var aDiv = document.getElementsByTagName( 'div' )[0];
alert( aDiv.classList.contains( 'a' ) );
判斷class里有沒有a,有的話返回true 沒有返回false;
if....else...高級操作
例:
if ( bool ){
obj.classList.add( 'on' );
} else {
obj.classList.remove( 'on' );
}
高級寫法
1扼倘、可看成
if ( bool ){
obj.classList[ 'add' ]( 'on' );
} else {
obj.classList[ 'remove' ]( 'on' );
}
2、三目寫法
obj.classList[ bool ? 'add' : 'remove' ]( 'on' );
另一種寫法:
var x = bool ? 'add' : 'remove';
obj.classList[ x ]( 'on' );