JS实现全选与全不选
一、步骤分析
1、使用<input type="checkbox" >进行复选框的布局。
2、对全选框进行事件绑定。(鼠标单击)
3、编写全选函数。
4、获取复选框的状态,使其他复选框与全选框的状态一致。
二、代码练习
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>隔行换色</title>
<script>
function chanclo(){
//获取标签
var arr= document.getElementsByTagName("tr");
//for循环实现换色
for(i=1;i<arr.length;i++){
//如果行数为偶数,背景颜色变为红色
if(i%2==0){
arr[i].style.backgroundColor="red";
}
//如果行数为奇数,背景颜色变为金色
else{
arr[i].style.backgroundColor="gold";
}
}
}
//编写全选中函数
function allch(obj){
//获取全选框的选中状态
var c1=obj.checked;
//获取其他复选框,返回的为数组
var c2=document.getElementsByClassName("ch1");
//通过for循环实现其他复选框的状态和全选框一致
for(var i=0;i<c2.length;i++){
c2[i].checked=c1;
}
}
</script>
</head>
<body οnlοad="chanclo()">
<table align="center" border="1" width="300px" height="300px">
<tr >
<th><input type="checkbox" class="ch1"></th>
<th>星期一</th>
<th>星期二</th>
<th>星期三</th>
</tr>
<tr>
<th><input type="checkbox" class="ch1"></th>
<td>语文</td>
<td>数学</td>
<td>英语</td>
</tr>
<tr>
<th><input type="checkbox" class="ch1"></th>
<td>语文</td>
<td>数学</td>
<td>英语</td>
</tr>
<tr>
<th><input type="checkbox" class="ch1"></th>
<td>语文</td>
<td>数学</td>
<td>英语</td>
</tr>
</table>
</body>
</html>
三、效果展示
备注:由于个人原因,本博客暂停更新。如有问题可联系本人,本人提供技术指导、学习方向、学习路线。本人微信wlp1156107728(添加注明来意) QQ1156107728(添加注明来意)