JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格效果的具體代碼,供大家參考,具體內(nèi)容如下

代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>動(dòng)態(tài)表格</title>
<style>
.bigDiv{
width: 600px;
margin: 50px auto;
}
table{
border: solid black 2px;
width: 500px;
/*邊框會(huì)合并為一個(gè)單一的邊框*/
border-collapse: collapse;
}
th{
background-color: darkgray;
/*表頭字體加粗*/
font-weight: bold;
/*字體顏色 #ffffff:白色*/
color: #ffffff;
}
th,td{
border: 1px solid black ;
/*指定的寬度和高度的行為。指定元素的寬度和高度(最小/最大屬性)適用于box的寬度和高度*/
box-sizing: content-box;
text-align: center;
/*指定寬高*/
width: 50px;
height: 30px;
font-size: 14px;
}
.but{
width: 150px;
margin: 5px 400px;
/*讓所有彈性盒模型對(duì)象的子元素都有相同的長(zhǎng)度,且忽略它們內(nèi)部的內(nèi)容*/
display: flex;
/*在彈性盒對(duì)象的 <div> 元素中的各項(xiàng)周圍留有空白*/
justify-content: flex-end;
}
</style>
</head>
<body>
<div class="bigDiv">
<table align="center">
<thead>
<tr>
<th>序號(hào)</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>聯(lián)系電話</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>逍遙</td>
<td>18</td>
<td>男</td>
<td>12354546</td>
</tr>
<tr>
<td>2</td>
<td>小白</td>
<td>19</td>
<td>女</td>
<td>252323523</td>
</tr>
</tbody>
</table>
<div class="but">
<button type="button" onclick="addRow()">添加一行</button>
<button type="button" onclick="saveData()">保存數(shù)據(jù)</button>
</div>
</div>
<script>
var id;
var name;
var age;
var sex;
var phone;
var tdArr=new Array();
function addRow() {
let tbodyObj = document.getElementsByTagName("tbody")[0];
let trObj = document.createElement("tr");
tbodyObj.appendChild(trObj);
//創(chuàng)建5個(gè)td
for (let i = 0; i < 5; i++) {
let tdObj = document.createElement("td");
trObj.appendChild(tdObj);
//創(chuàng)建input輸入框?qū)ο?
let inputObj = document.createElement("input");
//設(shè)置input對(duì)象的id屬性
inputObj.setAttribute("id",i);
//為每一個(gè)輸入框添加一個(gè)失去焦點(diǎn)事件
inputObj.addEventListener("blur",function () {
switch (this.id) {
case "0":
id=this.value;
break;
case "1":
name=this.value;
break;
case "2":
age=this.value;
break;
case "3":
sex=this.value;
break;
case "4":
phone=this.value;
break;
}
});
//隱藏未點(diǎn)擊輸入時(shí)的input邊框
inputObj.style.border="0";
//隱藏點(diǎn)擊輸入時(shí)的邊框
inputObj.style.outline="none";
//設(shè)置輸入框?qū)挾?
inputObj.style.width="80px";
//設(shè)置輸入框高度
inputObj.style.height="25px";
//設(shè)置輸入框的外邊距為0
inputObj.style.margin="0";
//將td添加
tdObj.appendChild(inputObj);
//將tdObj添加到tdArr中
tdArr.push(tdObj);
}
}
function saveData() {
tdArr[0].innerHTML=id;
tdArr[1].innerHTML=name;
tdArr[2].innerHTML=age;
tdArr[3].innerHTML=sex;
tdArr[4].innerHTML=phone;
tdArr.length=0;
}
</script>
</body>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 原生javaScript做得動(dòng)態(tài)表格(注釋寫的很清楚)
- js生成動(dòng)態(tài)表格并為每個(gè)單元格添加單擊事件的方法
- JS實(shí)現(xiàn)動(dòng)態(tài)表格的添加,修改,刪除功能(推薦)
- JavaScript實(shí)現(xiàn)簡(jiǎn)單動(dòng)態(tài)表格
- JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格的方法詳解
- JavaScript實(shí)現(xiàn)生成動(dòng)態(tài)表格和動(dòng)態(tài)效果的方法詳解
- java前端javascript生成動(dòng)態(tài)表格示例演示
- JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格的示例代碼
相關(guān)文章
require簡(jiǎn)單實(shí)現(xiàn)單頁(yè)應(yīng)用程序(SPA)
下面小編就為大家?guī)?lái)一篇require簡(jiǎn)單實(shí)現(xiàn)單頁(yè)應(yīng)用程序(SPA)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
Javascript 函數(shù)中的參數(shù)使用分析
關(guān)于JS中的函數(shù),相信大家已經(jīng)很了解了,其中有些特性呢,感覺(jué)還是值得提一提的,下面就說(shuō)說(shuō)JS中的函數(shù)吧。2010-03-03
微信小程序報(bào)錯(cuò):does?not?have?a?method?"xxxx"?to?ha
這篇文章主要給大家介紹了關(guān)于微信小程序報(bào)錯(cuò):does?not?have?a?method?"xxxx"?to?handle?event?"tap"的解決方案,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
JavaScript中將一個(gè)值轉(zhuǎn)換為字符串的方法分析[譯]
在JavaScript中,主要有三種方法能讓任意值轉(zhuǎn)換為字符串.本文講解了每種方法以及各自的優(yōu)缺點(diǎn)2012-09-09
利用百度地圖JSAPI生成h7n9禽流感分布圖實(shí)現(xiàn)代碼
本文將詳細(xì)介紹下如何使用百度地圖JSAPI生成的H7N9感染分布圖,有對(duì)百度api感興趣的朋友可以參考下哈,希望可以幫助到你2013-04-04
簡(jiǎn)單實(shí)現(xiàn)JS對(duì)dom操作封裝
這篇文章主要介紹了簡(jiǎn)單實(shí)現(xiàn)JS對(duì)dom操作封裝,需要的朋友可以參考下2015-12-12
微信小程序之滑動(dòng)頁(yè)面隱藏和顯示組件功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序之滑動(dòng)頁(yè)面隱藏和顯示組件功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

