JScript實(shí)現(xiàn)表格的簡(jiǎn)單操作
本文實(shí)例為大家分享了JScript實(shí)現(xiàn)表格的簡(jiǎn)單操作,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)思路:
1、添加時(shí):獲取當(dāng)前列表的行數(shù),在當(dāng)前一行添加下一行;
2、用insertCell()方法添加一行,下標(biāo)從0開始,
3、若要給新一行添加類型、響應(yīng)事件,就用setAttribute()方法,類似于鍵值對(duì),并用appendChild()方法將數(shù)據(jù)保存到新一行
4、刪除時(shí):獲取需要?jiǎng)h除行的當(dāng)前行數(shù)this,然后獲取父節(jié)點(diǎn),把整一行刪掉remove(),而不是單單刪除某一行的單個(gè)數(shù)據(jù)
5、修改時(shí):獲取當(dāng)前修改行的行數(shù)索引,點(diǎn)擊修改時(shí),把表格狀態(tài)轉(zhuǎn)換為文本格式,并把“修改”改為“確定”
實(shí)現(xiàn)代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> table{ border-top: 1px solid #ccc; border-left: 1px solid #ccc; width: 400px; } td,th{ border-right:1px solid #ccc ; border-bottom: 1px solid #ccc; } </style> <script> function add(){ var table = document.getElementById("order"); var index = table.rows.length;//表格行數(shù) var row = table.insertRow(index);//插入一個(gè)行并返回新一行 var c0 = row.insertCell(0); var b0 = document.createElement("input"); b0.setAttribute("type","checkbox"); b0.setAttribute("onclick","seclect("+index+")"); b0.setAttribute("name","sel"); c0.appendChild(b0); var c1 = row.insertCell(1);//在新一行插入一列,并返回新一列 c1.innerHTML = prompt("請(qǐng)輸入商品名稱",""); var c2 = row.insertCell(2);//在新一行插入一列,并返回新一列 c2.innerHTML = prompt("輸入數(shù)量",""); var c3 = row.insertCell(3);//在新一行插入一列,并返回新一列 c3.innerHTML = prompt("輸入價(jià)格",""); var c4 = row.insertCell(4); var b1 = document.createElement("input"); b1.setAttribute("type","button"); b1.setAttribute("value","刪除"); b1.setAttribute("onclick","del(this)"); var b2 = document.createElement("input");//創(chuàng)建按鈕 b2.setAttribute("type","button"); b2.setAttribute("value","修改"); b2.setAttribute("style","margin-left: 5px"); b2.setAttribute("onclick","update("+index+")"); c4.appendChild(b1);//把按鈕添加到操作的單元格中 c4.appendChild(b2); } function del(but){ //var table = document.getElementById("order"); but.parentNode.parentNode.remove();//根據(jù)節(jié)點(diǎn)的層級(jí)關(guān)系刪除行 } function update(index){ var table = document.getElementById("order"); //獲得修改按鈕 var cell=table.rows[index].cells[4]; cell.lastChild.setAttribute("value","確定"); //為按鈕重新綁定事件 cell.lastChild.setAttribute("onclick","edit("+index+")"); //修改數(shù)量 var cellNumer = table.rows[index].cells[2]; var txt = document.createElement("input"); //創(chuàng)建一個(gè)文本框 txt.setAttribute("value",cellNumer.innerHTML);//設(shè)置文本框的值 txt.setAttribute("size",5);//文本框長(zhǎng)度 cellNumer.innerHTML = "";//把單元格的數(shù)據(jù)清除 cellNumer.appendChild(txt); //把文本框加入到單元格 } function edit(index){ var table = document.getElementById("order"); var cell = table.rows[index].cells[4]; cell.lastChild.setAttribute("value","修改"); cell.lastChild.setAttribute("onclick","update("+index+")"); //把單元格中的文本框刪除 var cellNumer = table.rows[index].cells[2]; var num = cellNumer.firstChild.value;//取文本框的值 cellNumer.removeChild(cellNumer.firstChild);//刪除文本框 cellNumer.innerHTML = num; } function allSelect(ch){ var item = document.getElementsByTagName("input"); //取所有的input標(biāo)簽 for(var i=0;i<item.length;i++){ //循環(huán)每一個(gè) if(item[i].type==ch.type){ //判斷每一個(gè)標(biāo)簽的類型是否為CheckBox item[i].checked = ch.checked; //復(fù)選框的選中與全選的復(fù)選框選中相同 } } } function seclect(sh){ var item = document.getElementsByName("sel"); var all = document.getElementById("all"); var tag = true; for(var i=0;i<item.length;i++){//判斷是否全部選中 if(item[i].checked == false){ tag = false; break; } } all.checked = tag; } </script> </head> <body> <center> <table id="order" > <tr> <th> <input type="checkbox" onclick="allSelect(this)" id="all"/>全選 </th> <th>商品名稱</th> <th>數(shù)量</th> <th>單價(jià)</th> <th>操作</th> </tr> <tr> <td><input type="checkbox" onclick="seclect(this)" name="sel"/></td> <td>娃哈哈</td> <td>10</td> <td>2</td> <td><input value="刪除" type="button" onclick="del(this)"style="margin-right:5px ;"/><input value="修改" type="button" onclick="update(1)"/></td> </tr> </table> <button onclick="add()">添加商品</button> </center> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Javascript 表格操作實(shí)現(xiàn)代碼
- 基于javascript實(shí)現(xiàn)表格的簡(jiǎn)單操作
- javascript實(shí)現(xiàn)對(duì)表格元素進(jìn)行排序操作
- javascript實(shí)現(xiàn)表格增刪改操作實(shí)例詳解
- javascript操作表格排序?qū)嵗治?/a>
- JavaScript表格常用操作方法匯總
- JavaScript DOM操作表格及樣式
- javascript 表格內(nèi)容排序 簡(jiǎn)單操作示例代碼
- JavaScript動(dòng)態(tài)操作表格實(shí)例(添加,刪除行,列及單元格)
- javascript操作表格
相關(guān)文章
隨機(jī)顯示經(jīng)典句子或詩(shī)歌的javascript腳本
這篇文章主要介紹了隨機(jī)顯示經(jīng)典句子或詩(shī)歌的javascript腳本的相關(guān)資料,需要的朋友可以參考下2007-08-08JavaScript 解析數(shù)學(xué)表達(dá)式的過程詳解
這篇文章主要介紹了JavaScript 解析數(shù)學(xué)表達(dá)式的過程詳解,本文以一個(gè)的解題思路,來(lái)分享如何解決問題,解決的過程,可以作為解決工作中一般問題的通用思路,對(duì)js解析表達(dá)式相關(guān)知識(shí)感興趣的朋友一起看看吧2022-06-06jquery結(jié)合CSS使用validate實(shí)現(xiàn)漂亮的驗(yàn)證
這篇文章主要介紹了jquery結(jié)合CSS使用validate實(shí)現(xiàn)漂亮的驗(yàn)證,需要的朋友可以參考下2015-01-01基于BootStrap實(shí)現(xiàn)局部刷新分頁(yè)實(shí)例代碼
這篇文章主要介紹了基于BootStrap實(shí)現(xiàn)局部刷新的分頁(yè)的相關(guān)資料,非常不錯(cuò),代碼簡(jiǎn)單易懂,具有參考價(jià)值,需要的朋友可以參考下2016-08-08JS實(shí)現(xiàn)自適應(yīng)高度表單文本框的方法
這篇文章主要介紹了JS實(shí)現(xiàn)自適應(yīng)高度表單文本框的方法,實(shí)例分析了針對(duì)IE內(nèi)核與非IE內(nèi)核下的javascript控制文本框樣式的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02代碼實(shí)例ajax實(shí)現(xiàn)點(diǎn)擊加載更多數(shù)據(jù)圖片
在本篇文章里我們給大家分享了關(guān)于ajax實(shí)現(xiàn)點(diǎn)擊加載更多數(shù)據(jù)圖片的相關(guān)代碼知識(shí)點(diǎn),有興趣的朋友們參考下。2018-10-10