标签:char title col 通过 rip put classname sele nts
添加一个学生信息表格,表格的信息有编号、姓名、性别、信息勾选4个字段,可以任意的勾选其中0行或以上信息行。
<table> <caption>学生信息表</caption> <tr> <th><input type="checkbox" name="cb" id="firstCb"></th> <th>编号</th> <th>姓名</th> <th>性别</th> <th>操作</th> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>1</td> <td>令狐冲</td> <td>男</td> <td><a href="javascript:void(0);">删除</a></td> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>2</td> <td>任我行</td> <td>男</td> <td><a href="javascript:void(0);">删除</a></td> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>3</td> <td>岳不群</td> <td>?</td> <td><a href="javascript:void(0);">删除</a></td> </tr> </table>
<div> <input type="button" id="selectAll" value="全选"> <input type="button" id="unSelectAll" value="全不选"> <input type="button" id="selectReverse" value="反选"> </div>
table { /* 为表格添加边框(盒子模型) */ border: 1px solid; /* 表格宽度为500个像素点 */ width: 500px; /* 设置与table元素相关联的盒子模型的左外边距 */ margin-left: 30%; } td,th{ /* 为表格的每个单元格添加边框(盒子模型) */ border: 1px solid; /* 将每个单元格的文本信息向中对齐 */ text-align: center; } div { /* 设置与div元素相关联的盒子模型的左外边距 */ margin-left: 30%; /* 设置div元素的顶部外边距 */ margin-top: 10px; }
document.getElementById("selectAll").onclick = function() { // 通过标签name值,获取table标签中所有的checkbox对象 var checkBoxObject = document.getElementsByName("cb"); // 遍历 for (var i = 0; i < checkBoxObject.length; i++) { // 设置每一个checkbox的状态为选中 checked checkBoxObject[i].checked = true; } };
document.getElementById("unSelectAll").onclick = function() { // 通过标签name值,获取table标签中所有的checkbox对象 var checkBoxObject = document.getElementsByName("cb"); // 遍历 for (var i = 0; i < checkBoxObject.length; i++) { // 设置每一个checkbox的状态为选中 checked checkBoxObject[i].checked = false; } };
document.getElementById("firstCb").onclick = function() { // 获取所有的checkbox var checkBoxObject = document.getElementsByName("cb"); // 遍历 for (var i = 0; i < checkBoxObject.length; i++) { // 设置每一个cb的状态和第一个cb的状态一样 checkBoxObject[i].checked = this.checked; } };
var trs = document.getElementsByTagName("tr"); // 遍历 for (var i = 0; i < trs.length; i++) { // 移到元素之上 trs[i].onmouseover = function(){ this.className = "over"; }; // 移出元素 trs[i].onmouseout = function(){ this.className = "out"; }; }
用CSS样式,对该事件进行描述
.out{ background-color: white; } .over{ background-color: pink; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>表格信息勾选</title> <style> table { border: 1px solid; width: 500px; margin-left: 30%; } td,th{ border: 1px solid; text-align: center; } div { margin-left: 30%; margin-top: 10px; } .out{ background-color: white; } .over{ background-color: pink; } </style> <script> window.onload = function () { document.getElementById("selectAll").onclick = function() { var checkBoxObject = document.getElementsByName("cb"); for (var i = 0; i < checkBoxObject.length; i++) { checkBoxObject[i].checked = true; } }; document.getElementById("unSelectAll").onclick = function() { var checkBoxObject = document.getElementsByName("cb"); for (var i = 0; i < checkBoxObject.length; i++) { checkBoxObject[i].checked = false; } }; document.getElementById("selectReverse").onclick = function() { var checkBoxObject = document.getElementsByName("cb"); for (var i = 0; i < checkBoxObject.length; i++) { checkBoxObject[i].checked = !checkBoxObject[i].checked; } }; document.getElementById("firstCb").onclick = function() { var checkBoxObject = document.getElementsByName("cb"); for (var i = 0; i < checkBoxObject.length; i++) { checkBoxObject[i].checked = this.checked; } }; var trs = document.getElementsByTagName("tr"); for (var i = 0; i < trs.length; i++) { trs[i].onmouseover = function(){ this.className = "over"; }; trs[i].onmouseout = function(){ this.className = "out"; }; } } </script> </head> <body> <table> <caption>学生信息表</caption> <tr> <th><input type="checkbox" name="cb" id="firstCb"></th> <th>编号</th> <th>姓名</th> <th>性别</th> <th>操作</th> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>1</td> <td>令狐冲</td> <td>男</td> <td><a href="javascript:void(0);">删除</a></td> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>2</td> <td>任我行</td> <td>男</td> <td><a href="javascript:void(0);">删除</a></td> </tr> <tr> <td><input type="checkbox" name="cb" ></td> <td>3</td> <td>岳不群</td> <td>?</td> <td><a href="javascript:void(0);">删除</a></td> </tr> </table> <div> <input type="button" id="selectAll" value="全选"> <input type="button" id="unSelectAll" value="全不选"> <input type="button" id="selectReverse" value="反选"> </div> </body> </html>
HTML + CSS + JavaScript 实现勾选动态表格中的记录
标签:char title col 通过 rip put classname sele nts
原文地址:https://www.cnblogs.com/liyihua/p/12398325.html