JavaScript實(shí)現(xiàn)動(dòng)態(tài)生成表格案例詳解
前言
在這里實(shí)現(xiàn)一個(gè)動(dòng)態(tài)添加表格的案例,當(dāng)點(diǎn)擊添加按鈕時(shí),可以彈出一個(gè)表單,然后將輸入的內(nèi)容添加到表格中,也可以將表格中的整行內(nèi)容清除。
實(shí)現(xiàn)思路
先創(chuàng)建一個(gè)表格和一個(gè)表單,將表單中輸入的內(nèi)容動(dòng)態(tài)添加進(jìn)表格中,表單頁(yè)面右上角有一個(gè)關(guān)閉按鈕,當(dāng)點(diǎn)擊時(shí),可以將表單頁(yè)面關(guān)閉并將表格頁(yè)面顯示。為了頁(yè)面美觀,我將添加數(shù)據(jù)的按鈕放在了表格的<tfoot></tfoot>中,將動(dòng)態(tài)生成的表格數(shù)據(jù)添加到<tbody><tbody>中,當(dāng)點(diǎn)擊添加按鈕時(shí),隱藏表格,并顯示表單,在表單中填寫要添加的信息,然后獲取輸入的信息,通過(guò)jquery生成表格的一行元素,并將獲得的值添加進(jìn)去,最后將這一行添加到<tbody><tbody>的最后一行,當(dāng)點(diǎn)擊表單頁(yè)面的添加按鈕時(shí),讓表單隱藏,并顯示修改后的變革,因?yàn)檫€要實(shí)現(xiàn)動(dòng)態(tài)刪除功能,所以需要給表格中的每一行元素添加一個(gè)刪除屬性(超鏈接),當(dāng)我們點(diǎn)擊刪除時(shí),獲取到其對(duì)應(yīng)的行,進(jìn)行刪除操作。
實(shí)現(xiàn)代碼?
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
table {
width: 410px;
margin: 100px auto 0;
text-align: center;
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #ccc;
}
th,
td {
width:150px;
height: 40px;
border: 1px solid #ccc;
padding: 10px;
}
a{
text-decoration: none;
}
.btnAdd {
width: 110px;
height: 30px;
font-size: 20px;
}
.item {
position: relative;
padding-left: 100px;
padding-right: 20px;
margin-bottom: 34px;
}
.lb {
position: absolute;
left: 0;
top: 0;
display: block;
width: 100px;
text-align: right;
}
.txt {
width: 300px;
height: 32px;
}
.form-add {
position: absolute;
top: 100px;
left: 578px;
border: 1px solid #ccc;
margin-left: -197px;
padding-bottom: 20px;
display: none;
}
.title {
background-color: #f7f7f7;
border-width: 1px 1px 0 1px;
border-bottom: 0;
margin-bottom: 15px;
position: relative;
}
span {
width: auto;
height: 18px;
font-size: 16px;
color: rgb(102, 102, 102);
text-indent: 12px;
padding: 8px 0px 10px;
margin-right: 10px;
display: block;
overflow: hidden;
text-align: left;
}
.title div {
width: 16px;
height: 20px;
position: absolute;
right: 10px;
top: 6px;
font-size: 30px;
line-height: 16px;
cursor: pointer;
}
.submit {
text-align: center;
}
.submit input {
width: 170px;
height: 32px;
}
</style>
</head>
<body>
<!--按鈕和表單-->
<table>
<thead>
<tr>
<th>班級(jí)</th>
<th>姓名</th>
<th>學(xué)號(hào)</th>
<th>操作</th>
</tr>
</thead>
<tbody id="j_tb">
<tr>
<td>1班</td>
<td>小王</td>
<td>001</td>
<td><a href="javascrip:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="get">刪除</a></td>
</tr>
<tr>
<td>2班</td>
<td>小熊</td>
<td>002</td>
<td><a href="javascrip:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="get">刪除</a></td>
</tr>
</tbody>
<tfoot>
<tr>
<td id="j_btnAddData" class="btnAdd" colspan="4">添加數(shù)據(jù)</td>
</tr>
</tfoot>
</table>
<!--添加數(shù)據(jù)的表單-->
<div id="j_formAdd" class="form-add">
<div class="title">
<span>添加數(shù)據(jù)</span>
<div id="j_hideFormAdd">×</div>
</div>
<div class="item">
<label class="lb" for="">班級(jí):</label>
<input class="txt" type="text" id="classes" placeholder="請(qǐng)輸入班級(jí)">
</div>
<div class="item">
<label class="lb" for="">姓名:</label>
<input class="txt" type="text" id="uname" placeholder="請(qǐng)輸入姓名">
</div>
<div class="item">
<label class="lb" for="">學(xué)號(hào):</label>
<input class="txt" type="text" id="order" placeholder="請(qǐng)輸入學(xué)號(hào)">
</div>
<div class="submit">
<input type="button" value="添加" id="j_btnAdd">
</div>
</div>
</body>
</html>
<script src="jquery.js"></script>
<script>
$(function () {
$('#j_btnAddData').click(function () {
$('#j_formAdd').show();
$('table').hide()
});
$('#j_hideFormAdd').click(function () {
$('#j_formAdd').hide();
$('table').show()
});
$('#j_btnAdd').click(function () {
$('table').show()
$('#j_formAdd').hide();
var classes = $('#classes').val();
var uname = $('#uname').val();
var order = $('#order').val();
var New =$( '<tr>' +
'<td>'+classes+'</td>'+
'<td>'+uname+'</td>' +
'<td>'+order+'</td>' +
'<td><a href="javascrip:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="get">刪除</a></td>' +
'</tr>' );
$('#j_tb').append(New);
});
$('#j_tb').on('click','.get', function () {
$(this).parent().parent().remove();
});
});
</script>
實(shí)現(xiàn)效果
到此這篇關(guān)于JavaScript實(shí)現(xiàn)動(dòng)態(tài)生成表格案例詳解的文章就介紹到這了,更多相關(guān)JavaScript動(dòng)態(tài)生成表格內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
通過(guò)學(xué)習(xí)bootstrop導(dǎo)航條學(xué)會(huì)修改bootstrop顏色基調(diào)
這篇文章主要介紹了通過(guò)學(xué)習(xí)bootstrop導(dǎo)航條學(xué)會(huì)修改bootstrop顏色基調(diào),需要的朋友可以參考下2017-06-06微信小程序?qū)崿F(xiàn)星星評(píng)價(jià)效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)星星評(píng)價(jià)效果,支持多個(gè)條目評(píng)價(jià),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11JS與jQuery實(shí)現(xiàn)子窗口獲取父窗口元素值的方法
這篇文章主要介紹了JS與jQuery實(shí)現(xiàn)子窗口獲取父窗口元素值的方法,涉及javascript與jQuery操作窗口元素的相關(guān)技巧,需要的朋友可以參考下2017-04-04TypeScript?Pinia實(shí)戰(zhàn)分享(Vuex和Pinia對(duì)比梳理總結(jié))
這篇文章主要介紹了TypeScript?Pinia實(shí)戰(zhàn)分享(Vuex和Pinia對(duì)比梳理總結(jié)),今天我們?cè)賮?lái)實(shí)戰(zhàn)下官方推薦的新的vue狀態(tài)管理工具Pini,感興趣的小伙伴可以參考一下2022-06-06JS通過(guò)位運(yùn)算實(shí)現(xiàn)權(quán)限加解密
這篇文章主要介紹了JS通過(guò)位運(yùn)算實(shí)現(xiàn)權(quán)限加解密的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08JS實(shí)現(xiàn)超簡(jiǎn)單的漢字轉(zhuǎn)拼音功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)超簡(jiǎn)單的漢字轉(zhuǎn)拼音功能,結(jié)合實(shí)例形式分析了javascript漢字轉(zhuǎn)換成拼音的函數(shù)定義與使用技巧,需要的朋友可以參考下2016-12-12