JS實(shí)現(xiàn)簡單的表格增刪
更新時(shí)間:2020年01月16日 14:14:45 作者:SSSkyCong
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)簡單的表格增刪,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了JS實(shí)現(xiàn)表格增刪的具體代碼,供大家參考,具體內(nèi)容如下
描述:
JS——實(shí)現(xiàn)簡單的表格增刪。
效果:

實(shí)現(xiàn):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<!--4、編號(hào):文本框
書名:文本框
作者:文本框
出版社:文本框
添加按鈕
表格
編號(hào) 書名 作者 出版社 刪除-->
</head>
<body>
<div 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>
</div>
<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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- js實(shí)現(xiàn)簡單的打印表格
- JS表格的動(dòng)態(tài)操作完整示例
- Vue.js實(shí)現(xiàn)可編輯的表格
- Nodejs技巧之Exceljs表格操作用法示例
- vuejs+element UI table表格中實(shí)現(xiàn)禁用部分復(fù)選框的方法
- Layui數(shù)據(jù)表格 前后端json數(shù)據(jù)接收的方法
- JS/jQuery實(shí)現(xiàn)超簡單的Table表格添加,刪除行功能示例
- Vue.js實(shí)現(xiàn)可排序的表格組件功能示例
- vuejs+element UI點(diǎn)擊編輯表格某一行時(shí)獲取內(nèi)容填入表單的示例
- JS獲取表格視圖所選行號(hào)的ids過程解析
相關(guān)文章
Uniapp微信小程序?qū)崿F(xiàn)全局事件監(jiān)聽并進(jìn)行數(shù)據(jù)埋點(diǎn)的方法
niapp起源?uni-app是一個(gè)使用Vue.js開發(fā)所有前端應(yīng)用的框架,下面這篇文章主要給大家介紹了關(guān)于Uniapp微信小程序?qū)崿F(xiàn)全局事件監(jiān)聽并進(jìn)行數(shù)據(jù)埋點(diǎn)的相關(guān)資料,需要的朋友可以參考下2022-11-11
JS基于ES6新特性async await進(jìn)行異步處理操作示例
這篇文章主要介紹了JS基于ES6新特性async await進(jìn)行異步處理操作,結(jié)合實(shí)例形式分析了async await進(jìn)行異步處理操作的相關(guān)原理、步驟與注意事項(xiàng),需要的朋友可以參考下2019-02-02
javascript不同類型數(shù)據(jù)之間的運(yùn)算的轉(zhuǎn)換方法
這篇文章主要介紹了javascript不同類型數(shù)據(jù)之間的運(yùn)算的轉(zhuǎn)換方法,需要的朋友可以參考下2014-02-02

