第十六篇 JS实现全选操作
JS实现全选
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>JS全选,复选框</title>
</head>
<body>
<table id="mytable">
<tr>
<th><input type="checkbox" id="quan"/></th>
<th>序号</th>
<th>姓名</th>
<th>性别</th>
<th>地址</th>
</tr>
<tr>
<td><input type="checkbox"/></td>
<td>1</td>
<td>张三</td>
<td>女</td>
<td>四川省成都市</td>
</tr>
<tr>
<td><input type="checkbox"/></td>
<td>2</td>
<td>李斯</td>
<td>女</td>
<td>四川省成都市</td>
</tr>
<tr>
<td><input type="checkbox"/></td>
<td>3</td>
<td>玩玩玩</td>
<td>男</td>
<td>四川省成都市</td>
</tr>
<tr>
<td><input type="checkbox"/></td>
<td>4</td>
<td>哈哈哈</td>
<td>女</td>
<td>四川省成都市</td>
</tr>
</table>
<script>
// onload事件 页面加载完直接运行
window.onload = function () {
var mytable = document.getElementById("mytable");
var qq = document.getElementById("quan");
mytable.style.border = "1px solid gray";
mytable.style.borderCollapse = "collapse";
mytable.style.textAlign = "center";
mytable.style.width = "800px";
//以上是给table表格添加样式
//使用循环获取到表格的tr长度
for (i = 1; i < mytable.rows.length; i++) {
//因为复选框是tr里的第一个,所以cells里的数组下标为0
mytable.rows[i].cells[0].firstChild.onclick = function () {
//当我点击它,就做判断
//先判断它本身是被选中的就往下面走
if (this.checked) {
//如果它被选中我们就走进for循环
for (j = 1; j < mytable.rows.length; j++) {
//获取tr的长度,然后判断所有的复选框
if (!mytable.rows[j].cells[0].firstChild.checked) {
//如果这些复选框,有一个没有被选中,全选就变成未被选中
qq.checked = false;
return true;
//最后结束返回 true
}
}
//if判断,所有复选框都是选中的,就让全选变成true 变成选中
qq.checked=true;
}
else {
//如果它本身没有被选中,全选当然是变成false 未被选中
qq.checked = false;
}
}
}
//点击全选的复选框,将所有的复选框变成和它一样,要么选中,要么未选中
qq.onclick=function() {
for (i = 1; i < mytable.rows.length; i++) {
mytable.rows[i].cells[0].firstChild.checked = this.checked;
}
};
//以下是循环并判断表格里的tr和td,添加css样式
for (i = 0; i < mytable.rows.length; i++) {
if (i == 0) {
mytable.rows[0].style.backgroundColor = "lightgray";
} else {
mytable.rows[i].onmouseover = function () {
this.style.backgroundColor = "red";
};
mytable.rows[i].style.cursor = "pointer";
if (i % 2 == 0) {
mytable.rows[i].style.backgroundColor = "yellow";
mytable.rows[i].onmouseout = function () {
this.style.backgroundColor = "yellow";
}
} else {
mytable.rows[i].style.backgroundColor = "orange";
mytable.rows[i].onmouseout = function () {
this.style.backgroundColor = "orange";
}
}
}
for (j = 0; j < mytable.rows[i].cells.length; j++) {
var c = mytable.rows[i].cells[j];
c.style.border = "1px solid gray";
}
}
}
</script>
</body>
</html>
第十六篇 JS实现全选操作的更多相关文章
- 利用js写全选操作
<script type="text/javascript"> function checkall(qx) { var ck=document.getElementsB ...
- Python之路【第十六篇】:Django【基础篇】
Python之路[第十六篇]:Django[基础篇] Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了O ...
- 跟我学SpringCloud | 第十六篇:微服务利剑之APM平台(二)Pinpoint
目录 SpringCloud系列教程 | 第十六篇:微服务利剑之APM平台(二)Pinpoint 1. Pinpoint概述 2. Pinpoint主要特性 3. Pinpoint优势 4. Pinp ...
- Egret入门学习日记 --- 第十六篇(书中 6.10~7.3节 内容)
第十六篇(书中 6.10~7.3节 内容) 昨天搞定了6.9节,今天就从6.10节开始. 其实这个蛮简单的. 这是程序员模式. 这是设计师模式. 至此,6.10节 完毕. 开始 6.11节. 有点没营 ...
- 解剖SQLSERVER 第十六篇 OrcaMDF RawDatabase --MDF文件的瑞士军刀(译)
解剖SQLSERVER 第十六篇 OrcaMDF RawDatabase --MDF文件的瑞士军刀(译) http://improve.dk/orcamdf-rawdatabase-a-swiss-a ...
- JS checkbox 全选 全不选
/* JS checkbox 全选 全不选 Html中checkbox: <input type="checkbox" name="cbx" value= ...
- JS实现全选、反选、不选
JS实现全选.反选.不选 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- elementUI+JS实现全选与反选
在实际项目开发过程中,遇到的需求,需要实现全选以及取消全选等功能,主要使用ElementUI + JS来实现,具体代码如下: <!DOCTYPE html> <html lang=& ...
- JS实战 · 复选框全选操作
思路: 1.获取被选中checkbox,通过checked属性的状态完成: 2.获取被选中的checkbox的value值: 3.求所有value的和sum: 4.定义span区域存储和sum: ...
随机推荐
- pandas.Series.value_counts
pandas.Series.value_counts Series.value_counts(normalize=False, sort=True, ascending=False, bins=Non ...
- Excel中,如何将人名按姓和名分开?
在Excel中,怎么将姓名分开呢? 用到三个函数: left函数:从文本字符串的左端开始,返回指定个数的字符: right函数:从字符串右端开始,返回指定个数的字符: len函数:返回文本串的字符数 ...
- nacos 使用笔记
启动命令: 单机模式启动 start.sh -m standalone
- JxBrowser开启调试模式,JxBrowser debug
原文: 一.问题描述 像一般的浏览器都带了调试功能,按F12就能打开,在JxBrowser中如何开启调试模式了. 二.解决方法 以下代码就能开启调试模式: import com.teamdev.jxb ...
- OpenCV学习笔记(14)——轮廓的性质
提取一些经常使用的对象特征 1.长宽比 边界矩形的宽高比 x,y,w,h = cv2.boundingRect(cnt) a ...
- mysql数据库基本操作sql语言
mysql的启动与关闭 启动 /etc/init.d/mysql start 多实例使用自建脚本启动 2种关闭数据库方法 mysqladmin -uroot -p密码 shutdown #优雅关闭/e ...
- ABAP Field+offset字符串截取
*删除字符串末尾的字符 DATA:str TYPE string VALUE 'abc,defg,', sub_str TYPE string, num TYPE i. WRITE:/ str. nu ...
- Redis在window上的安装
1 Redis安装 Redis 没有官方的Windows版本,但是微软开源技术团队(Microsoft Open Tech group)开发和维护着这个 Win64 的版本. 在github上面可以下 ...
- weak_ptr用法
http://note.youdao.com/noteshare?id=39c10fda83320b2851e2073a5f69321c
- HBase 参考信息
Apache HBase Region Splitting and Merging https://blog.cloudera.com/apache-hbase-region-splitting-a ...