js實(shí)現(xiàn)單擊可修改表格
純js實(shí)現(xiàn)單擊可修改表格(類(lèi)似成績(jī)單),供大家參考,具體內(nèi)容如下
功能:實(shí)現(xiàn)成績(jī)單單擊表格可對(duì)數(shù)據(jù)進(jìn)行修改且對(duì)輸入的數(shù)字大小有驗(yàn)證例如不小于0不大于100,總分欄會(huì)對(duì)總分進(jìn)行求和(利用了es6的模板字符串)。
實(shí)現(xiàn)效果:

代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table{
margin: 0 auto;
z-index:999999;
border-collapse: collapse;
}
th {
background-color: #4CAF50;
/* background-image: linear-gradient(to right, #eea2a2 0%, #bbc1bf 19%, #57c6e1 42%, #b49fda 79%, #7ac5d8 100%); */
color: white;
}
table th,tr,td{
width:100px;
text-align: center;
}
table input{
border:none;
outline: none;
width: 100%;
}
.inputClass{
width:80px;
height:100%
}
</style>
</head>
<body>
<div style="position: relative;margin-top: 200px;text-align:center">
<h2 style="margin-bottom: 50px;">成績(jī)登記表</h2>
<div >
<table border="1">
<thead>
<th>學(xué)號(hào)</th>
<th>姓名</th>
<th>語(yǔ)文</th>
<th>數(shù)學(xué)</th>
<th>英語(yǔ)</th>
<th>總分</th>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</body>
<script>
// 數(shù)組
let data = [
{
id:1101,
name:'小王',
Chinese:100,
Math:80,
English:91,
total:271
},
{
id:1102,
name:'小曾',
Chinese:88,
Math:87,
English:92,
total:267
},
{
id:1103,
name:'小趙',
Chinese:75,
Math:20,
English:86,
total:181
},
{
id:1104,
name:'小周',
Chinese:65,
Math:81,
English:83,
total:229
}
]
window.onload = function(){
initdata()
}
//初始化數(shù)據(jù)
// 模板填入數(shù)據(jù)
function initdata(){
let tbodyinner = document.getElementsByTagName("tbody")[0]
let html = ''
for(let i=0;i<data.length;i++){
html+=`
<tr>
<td>${data[i].id}</td>
<td>${data[i].name}</td>
<td name="grade" class="chinese">${data[i].Chinese}</td>
<td name="grade" class="math">${data[i].Math}</td>
<td name="grade" class="english">${data[i].English}</td>
<td class="allscore">${parseInt(data[i].Chinese)+parseInt(data[i].Math)+parseInt(data[i].English)}</td>
</tr>
`
}
// tbody.innerHTML="..."往tbody中添加內(nèi)容
tbodyinner.innerHTML = html
getNode()
}
// 監(jiān)聽(tīng)click事件
function getNode(){
let subject = document.getElementsByName("grade")
for(let i=0;i<subject.length;i++){
subject[i].addEventListener('click',function(){
edit(this)
})
}
}
//鼠標(biāo) 移入點(diǎn)
function edit(i){
let inputlen = document.getElementsByTagName('input').length
let oldvalue = i.innerHTML
if(inputlen==0){
// 設(shè)置該標(biāo)簽為空
i.innerHTML = ''
// 添加input對(duì)象
let inp = document.createElement("input")
inp.value = oldvalue
inp.classList.add("inputClass")
// 添加子節(jié)點(diǎn)
i.appendChild(inp)
// 獲取文本中的內(nèi)容
inp.select()
// 失去焦點(diǎn)事件
inp.onblur = function(){
if(inp.value<=100&&inp.value>=0){
i.innerHTML = inp.value
let val1 = i.parentNode.childNodes[5].innerHTML
let val2 = i.parentNode.childNodes[7].innerHTML
let val3 = i.parentNode.childNodes[9].innerHTML
i.parentNode.childNodes[11].innerHTML = parseInt(val1)+parseInt(val2)+parseInt(val3)
}else{
return alert("數(shù)據(jù)值不對(duì),請(qǐng)重新輸入!");
}
}
}
}
</script>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- angularjs實(shí)現(xiàn)table表格td單元格單擊變輸入框/可編輯狀態(tài)示例
- js生成動(dòng)態(tài)表格并為每個(gè)單元格添加單擊事件的方法
- vuejs element table 表格添加行,修改,單獨(dú)刪除行,批量刪除行操作
- angularJs 表格添加刪除修改查詢方法
- JS實(shí)現(xiàn)動(dòng)態(tài)表格的添加,修改,刪除功能(推薦)
- JS動(dòng)態(tài)修改表格cellPadding和cellSpacing的方法
- js動(dòng)態(tài)修改表格行colspan列跨度的方法
- javascript修改表格背景色實(shí)例代碼分享
- 查詢綁定數(shù)據(jù)島的表格中的文本并修改顯示方式的js代碼
相關(guān)文章
使用JS進(jìn)行目錄上傳(相當(dāng)于批量上傳)
腳本使用了WScript.Shell和Scripting.FileSystemObject的組件,所以必須要在IE下面和打開(kāi)安全選項(xiàng)中運(yùn)行; 另外還用到了Jquery. 代碼只是客戶端代碼, 至于服務(wù)器的接收代碼網(wǎng)上好多了2010-12-12
script標(biāo)簽中的defer和async使用技巧說(shuō)明
這篇文章主要介紹了script標(biāo)簽中的defer和async使用技巧,包含他們的下載順序和執(zhí)行順序,以及使用場(chǎng)景需要的朋友可以參考下2023-02-02
js中eval方法詳解之eval方法的初級(jí)應(yīng)用
js中eval()函數(shù)可計(jì)算某個(gè)字符串,下面這篇文章主要給大家介紹了關(guān)于js中eval方法詳解之eval方法的初級(jí)應(yīng)用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
javascript Error 對(duì)象 錯(cuò)誤處理
javascript常見(jiàn)error對(duì)象處理,錯(cuò)誤代碼集合2008-05-05
Javascript動(dòng)畫(huà)的實(shí)現(xiàn)原理淺析
這篇文章主要介紹了Javascript動(dòng)畫(huà)的實(shí)現(xiàn)原理淺析,本文用兩個(gè)實(shí)例來(lái)解釋Javascript動(dòng)畫(huà)的實(shí)現(xiàn)原理,需要的朋友可以參考下2015-03-03
php對(duì)mongodb的擴(kuò)展(小試牛刀)
本文主要講解php連接、操作mongodb,有需求的朋友可以參考下2012-11-11
使用javascript實(shí)現(xiàn)頁(yè)面定時(shí)跳轉(zhuǎn)總結(jié)篇
下面對(duì)使用JavaScript實(shí)現(xiàn)頁(yè)面定時(shí)跳轉(zhuǎn)做一下總結(jié),各種定時(shí)跳轉(zhuǎn)代碼記錄如下,希望對(duì)大家有所幫助2013-09-09

