JavaScript獲取表格(table)當(dāng)前行的值、刪除行、增加行
更新時(shí)間:2015年07月03日 10:25:46 投稿:junjie
這篇文章主要介紹了JavaScript獲取表格(table)當(dāng)前行的值、刪除行、增加行,本文直接給出代碼示例,需要的朋友可以參考下
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>Js獲取 table當(dāng)前行的值</title> <script language=javascript> var selectedTr = null; function c1(obj) { obj.style.backgroundColor = 'blue'; //把點(diǎn)到的那一行變希望的顏色; if (selectedTr != null) selectedTr.style.removeAttribute("backgroundColor"); if (selectedTr == obj) selectedTr = null;//加上此句,以控制點(diǎn)擊變白,再點(diǎn)擊反灰 else selectedTr = obj; } /*得到選中行的第一列的值*/ function check() { if (selectedTr != null) { var str = selectedTr.cells[0].childNodes[0].value; document.getElementById("lab").innerHTML = str; } else { alert("請(qǐng)選擇一行"); } } /*刪除選中行*/ function del() { if (selectedTr != null) { if (confirm("確定要?jiǎng)h除嗎?")) { alert(selectedTr.cells[0].childNodes[0].value); var tbody = selectedTr.parentNode; tbody.removeChild(selectedTr); } } else { alert("請(qǐng)選擇一行"); } } </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>
您可能感興趣的文章:
- JS實(shí)現(xiàn)動(dòng)態(tài)生成html table表格的方法分析
- js生成動(dòng)態(tài)表格并為每個(gè)單元格添加單擊事件的方法
- JS實(shí)現(xiàn)動(dòng)態(tài)生成表格并提交表格數(shù)據(jù)向后端
- JS控制網(wǎng)頁動(dòng)態(tài)生成任意行列數(shù)表格的方法
- js動(dòng)態(tài)生成指定行數(shù)的表格
- JS實(shí)現(xiàn)從表格中動(dòng)態(tài)刪除指定行的方法
- JS對(duì)HTML表格進(jìn)行增刪改操作
- 動(dòng)態(tài)添加刪除表格行的js實(shí)現(xiàn)代碼
- 基于JavaScript實(shí)現(xiàn)動(dòng)態(tài)添加刪除表格的行
- JavaScript動(dòng)態(tài)生成帶刪除行功能的表格
相關(guān)文章
javascript:window.open彈出窗口的位置問題
這篇文章主要介紹了javascript:window.open彈出窗口的位置問題,需要的朋友可以參考下2014-03-03使用openSpeDiv方法實(shí)現(xiàn)Ecshop登錄彈窗框效果
在ECSHOP的目錄/JS/common.js中有一個(gè)openSpeDiv方法是實(shí)現(xiàn)ECSHOP的彈窗效果的。接下來通過本文給大家分享使用openSpeDiv方法實(shí)現(xiàn)Ecshop登錄彈窗框效果,需要的朋友參考下2017-03-03JavaScript仿小米實(shí)現(xiàn)球體分解動(dòng)畫
用過小米手機(jī)的應(yīng)該見過它的垃圾清理ui界面吧,本文將利用JavaScript模擬這一界面實(shí)現(xiàn)球體分解動(dòng)畫,感興趣的小伙伴可以學(xué)習(xí)一下2022-06-06request請(qǐng)求獲取參數(shù)的實(shí)現(xiàn)方法(post和get兩種方式)
下面小編就為大家?guī)硪黄猺equest請(qǐng)求獲取參數(shù)的實(shí)現(xiàn)方法(post和get兩種方式)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09設(shè)置jsf的選擇框h:selectOneMenu為不可編輯狀態(tài)的方法
本文為大家詳細(xì)介紹下如何設(shè)置jsf的選擇框h:selectOneMenu為不可編輯狀態(tài),具體實(shí)現(xiàn)代碼如下,希望對(duì)大家有所幫助2014-01-01