JavaScript擷取表格(table)當前行的值、刪除行、增加行,javascripttable

來源:互聯網
上載者:User

JavaScript擷取表格(table)當前行的值、刪除行、增加行,javascripttable

<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>Js擷取 table當前行的值</title><script language=javascript>var selectedTr = null;function c1(obj) {obj.style.backgroundColor = 'blue'; //把點到的那一行變希望的顏色; if (selectedTr != null)selectedTr.style.removeAttribute("backgroundColor");if (selectedTr == obj)selectedTr = null;//加上此句,以控制點擊變白,再點擊反灰 elseselectedTr = obj;}/*得到選中行的第一列的值*/function check() {if (selectedTr != null) {var str = selectedTr.cells[0].childNodes[0].value;document.getElementById("lab").innerHTML = str;} else {alert("請選擇一行");}}/*刪除選中行*/function del() {if (selectedTr != null) {if (confirm("確定要刪除嗎?")) {alert(selectedTr.cells[0].childNodes[0].value);var tbody = selectedTr.parentNode;tbody.removeChild(selectedTr);}} else {alert("請選擇一行");}}</script></head><body>單擊選中Tr,高亮顯示,再單擊取消選選中。<input type=button value="選中的是哪一行?" onclick="check()"><input type=button value="刪除選中行" onclick="del()"><input type=button value="增加一行" onclick="add()"><table width="100%" border="1" cellspacing="0" cellpadding="0" id="tab"><tr onclick="c1(this);" bgcolor="#cccccc"><td><input type="text" value="11"></td><td><input type="text" value="12"></td></tr><tr onclick="c1(this);" bgcolor="#e0e0e0"><td><input type="text" value="21"></td><td><input type="text" value="22"></td></tr><tr onclick="c1(this);" bgcolor="#cccccc"><td><input type="text" value="31"></td><td><input type="text" value="32"></td></tr><tr onclick="c1(this);" bgcolor="#e0e0e0"><td><input type="text" value="41"></td><td><input type="text" value="42"></td></tr><tr onclick="c1(this);" bgcolor="#cccccc"><td><input type="text" value="51"></td><td><input type="text" value="52"></td></tr></table><label id="lab"></label></body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.