您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)JavaScript中怎么實(shí)現(xiàn)表格增刪操作,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
實(shí)現(xiàn):
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><!--4、編號(hào):文本框書名:文本框作者:文本框出版社:文本框添加按鈕表格編號(hào) 書名 作者 出版社 刪除--></head> <body> <p id="all"> <p>編 號(hào):<input type="text" class="in"></p> <p>書 名:<input type="text" class="in"></p> <p>作 者:<input type="text" class="in"></p> <p>出版社 :<input type="text" class="in"></p> <p> <input type="button" value="添加" id="tj"> <input type="button" value="清除" id="cle"> </p> </p> <script> var ins,tj,qc,tab,all; var bookData=["編號(hào)","書名","作者","出版社","刪除"]; init(); function init() { all=document.getElementById("all"); ins=document.getElementsByClassName("in"); tj=document.getElementById("tj"); qc=document.getElementById("cle"); del=document.getElementById("del"); tj.addEventListener("click",clickHandler); qc.addEventListener("click",clickQcHandler); creatTable(); } function creatTable() { tab = $c("table", all, { width:"500px", borderCollapse:"collapse" }); for (var i = 0; i < ins.length+1; i++) {//購物車表單數(shù)據(jù)賦值 var th = $c("th", tab, { textAlign: "center", border: "1px solid #000000" }); th.textContent=bookData[i]; } } function clickHandler() { var tr = $c("tr", tab, { textAlign: "center", border: "1px solid #000000" }); for (var i = 0; i < ins.length+1; i++) {//購物車表單數(shù)據(jù)賦值 var td = $c("td", tr, {//列的創(chuàng)建 textAlign: "center", border: "1px solid #000000" }); if(i<ins.length){ td.textContent = ins[i].value; } else if(i==ins.length){ var del=$c("button", td); del.textContent="刪除"; del.addEventListener("click",clickDelHandler); } } } function clickDelHandler(e) {//刪除 一行 this.parentNode.parentNode.remove(); } function clickQcHandler(e) {//清除 全部 tab.remove(); creatTable(); } function $c(type,parent,style) { var elem=document.createElement(type); if(parent) parent.appendChild(elem); for(var key in style){ elem.style[key]=style[key]; } return elem; } </script> </body></html>
上述就是小編為大家分享的JavaScript中怎么實(shí)現(xiàn)表格增刪操作了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。