病毒横行,在家继续学习~
今天做一个不到20行 JS 代码的全选特效。先看效果:
*{
margin: 0;
padding: 0;
}
input{
vertical-align: middle;
}
.head{
width: 500px;
margin-left: auto;
margin-right: auto;
margin-top: 100px;
padding-bottom: 10px;
}
.content{
width: 500px;
margin-left: auto;
margin-right: auto;
}
.content td{
border-bottom:1px #666 dashed;
padding-top: 10px;
padding-bottom: 10px;
}
<div class="head">
<label class="myradio" id="chooseAll">
<input type="checkbox" name="chooseAll">
<span>全选</span>
</label>
</div>
<div class="content" id="content">
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="50">
<label class="choose_item_label">
<input type="checkbox" class="choose_item">
</label>
</td>
<td>
模拟内容
</td>
</tr>
<tr>
<td width="50">
<label class="choose_item_label">
<input type="checkbox" class="choose_item">
</label>
</td>
<td>
模拟内容
</td>
</tr>
<tr>
<td width="50">
<label class="choose_item_label">
<input type="checkbox" class="choose_item">
</label>
</td>
<td>
模拟内容
</td>
</tr>
</table>
</div>
let chooseAll = document.getElementById("chooseAll");
let content = document.getElementById("content");
let itemLabel = content.querySelectorAll(".choose_item_label");
let chooseAllFun = function(){
for(let i=0 ; i <= itemLabel.length-1 ; i++ ){
itemLabel.children[0].checked = chooseAll.children[0].checked;
}
};
chooseAll.addEventListener("click",chooseAllFun);
let chooseItemFun = function(){
if( !this.children[0].checked ){
chooseAll.children[0].checked = false ;
}
};
for( let i=0 ; i<= itemLabel.length-1 ; i++ ){
itemLabel.addEventListener("click", chooseItemFun);
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |