javascript實現可全選、反選及刪除表格的方法

來源:互聯網
上載者:User

 本文執行個體講述了javascript實現可全選、反選及刪除表格的方法。分享給大家供大家參考。具體實現方法如下:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>可以全選反選刪除的表格</title> <style type="text/css"> body,table { margin:0; padding:0; font-size:14px; } table,tr,th,td { border:1px solid #cdc; } th { background-color:green; width:100px; } .oddColor { background-color:#ccc; } .evenColor { background-color:#fcf; } .overColor { background-color:#dff; } </style> <script type="text/javascript"> //定義嵌套數組 var datas = [["張三", 30, "南昌"], ["李四", 25, "北京"], ["王五", 20, "鄭州"], ["趙六", 19, "武漢"], ["李莫", 18, "深圳"], ["羅成", 33, "重慶"], ["王平", 31, "天津"], ["劉萍", 22, "上海"], ["楊麗", 17, "石家莊"], ["郭麗", 30, "廣州"]]; //動態建立表格 function CreateMyTable() { var tblMain = document.getElementById("tblMain"); var rowsCount = tblMain.rows.length; var addRow; var addCol; var detailInfos; for (var i = 0; i < datas.length; i++) { addRow = tblMain.insertRow(rowsCount - 1);//控制資料行添加到倒數第二列 addCol = addRow.insertCell(-1); addCol.innerHTML = "<input type='checkbox' name='item' />"; addCol.align = "center"; //控制列置中 detailInfos = datas[i]; for (var j = 0; j < detailInfos.length; j++) { addCol = addRow.insertCell(-1); addCol.innerHTML = detailInfos[j]; } addCol = addRow.insertCell(-1); addCol.innerHTML = "<input type='button' id='btnDel" + i + "' value='刪除' onclick='btnDel(this)' />"; addCol.align = "center"; //控制列置中 rowsCount++; } TableColor(); } //設定網格間隔色和高亮顯示 var oldClassName; //記住行的背景色 function TableColor() { var tblMain = document.getElementById("tblMain"); var rowNodes = tblMain.rows; for (var i = 1; i < rowNodes.length-1; i++) { //除去首行和末行 if (i % 2 == 0) { rowNodes[i].className = "evenColor"; } else { rowNodes[i].className = "oddColor"; } rowNodes[i].onmouseover = function () { oldClassName = this.className; this.className = "overColor"; } rowNodes[i].onmouseout = function () { this.className = oldClassName; } } } //複選框全選函數 function checkAll() { var currentCheckNode = event.srcElement; var checkAllNodes = document.getElementsByName("all"); //把沒有點擊的全選複選框去除複選 for (var i = 0; i < checkAllNodes.length; i++) { if (currentCheckNode != checkAllNodes[i]) { checkAllNodes[i].checked = false; } } var checkItemNodes = document.getElementsByName("item"); for (var i = 0; i < checkItemNodes.length; i++) { checkItemNodes[i].checked = currentCheckNode.checked; } } //按鈕選擇函數 function btnCheckboxSel(index) { var checkItemNodes = document.getElementsByName("item"); for (var i = 0; i < checkItemNodes.length; i++) { if (index == 2) { checkItemNodes[i].checked = !checkItemNodes[i].checked; } else { checkItemNodes[i].checked = index; } } } //每行的刪除按鈕函數 function btnDel(btn) { var tblMain = document.getElementById("tblMain"); var delRowNode = btn.parentNode.parentNode; var sMsg = "您是否要刪除 姓名為:【" + delRowNode.cells[1].innerText + "】,年齡為:【" + delRowNode.cells[2].innerText + "】,城市為:【" + delRowNode.cells[3].innerText + "】 的資料?"; if (window.confirm(sMsg)) { tblMain.tBodies[0].removeChild(delRowNode); TableColor(); } } //刪除所選項按鈕函數 function btnDelSelectRow() { var arrDel = new Array(); var pos = 0; var itemNodes = document.getElementsByName("item"); for (var i = 0; i < itemNodes.length; i++) { if (itemNodes[i].checked) { arrDel[pos] = itemNodes[i].parentNode.parentNode; pos++; } } if (pos <= 0) { return; } if (!window.confirm("是否要刪除選擇的資料?")) return; var tblMain = document.getElementById("tblMain"); for (var i = 0; i < arrDel.length; i++) { tblMain.tBodies[0].removeChild(arrDel[i]); } } window.onload = CreateMyTable; </script> </head> <body> <table id="tblMain" cellspacing="0" cellpadding="10px" align="center"> <tbody> <tr> <th><input type="checkbox" name="all" onclick="checkAll()" />全選</th> <th>姓名</th> <th>年齡</th> <th>城市</th> <th>操作</th> </tr> <tr> <th><input type="checkbox" name="all" onclick="checkAll()" />全選</th> <th colspan="4"> <input type="button" value="全選" onclick="btnCheckboxSel(1)" /> <input type="button" value="全清" onclick="btnCheckboxSel(0)" /> <input type="button" value="反選" onclick="btnCheckboxSel(2)" /> <input type="button" value="刪除所選項" onclick="btnDelSelectRow()" /> </th> </tr> </tbody> </table> </body> </html>

希望本文所述對大家的javascript程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.