用JS做了一个成绩表自动求值,感觉太麻烦了,感觉应该有历遍数组的简单方法,但是我有点蒙圈,有没有人能教教我。
<div class="container">
<em> ** 请在下表中输入各科成绩 **</em>
<table class="box">
<tr>
<th id="title" colspan="7";>成 绩 表</th>
</tr>
<tr>
<th> </th>
<th>Jeck</th>
<th>Tom</th>
<th>Alic</th>
<th>Smith</th>
<th>Bosh</th>
<th>Lili</th>
</tr>
<tr>
<th>语文</th>
<td><input type="text" id="yj" oninput="Jeck()"></td>
<td><input type="text" id="yt" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
<tr>
<th>数学</th>
<td><input type="text" id="sj" oninput="Jeck()"></td>
<td><input type="text" id="st" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
…………
function Jeck() {
let ja = document.getElementById("yj").value;
let jb = document.getElementById("sj").value;
let jc = document.getElementById("ej").value;
let jd = document.getElementById("tj").value;
let je = document.getElementById("mj").value;
let jf = document.getElementById("aj").value;
let jx = document.getElementById("ttj").value = (Number(ja) +Number(jb)+Number(jc)
+Number(jd)+Number(je) +Number(jf));
document.getElementById("vj").value = Math.round(jx/6);
}
function Tom() {
let ta = document.getElementById("yt").value;
let tb = document.getElementById("st").value;
let tc = document.getElementById("et").value;
let td = document.getElementById("tt").value;
let te = document.getElementById("mt").value;
let tf = document.getElementById("at").value;
let tx = document.getElementById("ttt").value = (Number(ta) +Number(tb)+Number(tc)
+Number(td)+Number(te) +Number(tf));
document.getElementById("vt").value = Math.round(tx/6);
}
基础代码
<style>
*{
margin: 0;
height: 0;
}
em{
font-size: 8px;
}
.container{
position: relative;
width: 900px;
height: 600px;
background-color:aqua;
margin: 50px auto;
}
.box{
width: 800px;
height: 500px;
position: absolute;
margin: 10px 30px;
}
#title{
color:red;
font-size:28px;
text-align:center;
}
td,th{
height: 50px;
line-height: 50px;
font-size: 20px;
text-align:center;
/* border: 1px solid rgb(3, 65, 3); */
box-sizing: border-box;
}
input{
width: 80px;
height: 20px;
text-align: center;
font-size: 20px;
color:rgb(164, 5, 5) ;
border: 1px solid #ccc;
border-radius: 5px;
box-shadow: inset 0px 0px 5px;
background-color: rgb(98, 190, 106);
}
#average,#total{
width: 80px;
height: 23px;
text-align: center;
font-size: 22px;
color:rgb(116, 25, 26) ;
border: 1px solid #ccc;
border-radius: 10px;
box-shadow: inset 0px 0px 5px;
background-color: rgb(17, 159, 230);
}
</style>
```html
</head>
<body>
<div class="container">
<em> ** 请在下表中输入各科成绩 **</em>
<table class="box">
<tr>
<th id="title" colspan="7";>成 绩 表</th>
</tr>
<tr>
<th> </th>
<th>Jeck</th>
<th>Tom</th>
<th>Alic</th>
<th>Smith</th>
<th>Bosh</th>
<th>Lili</th>
</tr>
<tr>
<th>语文</th>
<td><input type="text" id="yj" oninput="Jeck()"></td>
<td><input type="text" id="yt" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
<tr>
<th>数学</th>
<td><input type="text" id="sj" oninput="Jeck()"></td>
<td><input type="text" id="st" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
<tr>
<th>英语</th>
<td><input type="text" id="ej" oninput="Jeck()"></td>
<td><input type="text" id="et" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
<tr>
<th>体育</th>
<td><input type="text" id="tj" oninput="Jeck()"></td>
<td><input type="text" id="tt" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
<tr>
<th>音乐</th>
<td><input type="text" id="mj" oninput="Jeck()"></td>
<td><input type="text" id="mt" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
<tr>
<th>美术</th>
<td><input type="text" id="aj" oninput="Jeck()"></td>
<td><input type="text" id="at" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
<tr id="average">
<th >平均分</th>
<td><input type="text" id="vj" oninput="Jeck()"></td>
<td><input type="text" id="vt" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
<tr id="total">
<th >总成绩</th>
<td><input type="text" id="ttj" oninput="Jeck()"></td>
<td><input type="text" id="ttt" oninput="Tom()"></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
<td><input type="text" id=""></td>
</tr>
</table>
</div>
</body>
```javascript
function Jeck() {
let ja = document.getElementById("yj").value;
let jb = document.getElementById("sj").value;
let jc = document.getElementById("ej").value;
let jd = document.getElementById("tj").value;
let je = document.getElementById("mj").value;
let jf = document.getElementById("aj").value;
let jx = document.getElementById("ttj").value = (Number(ja) +Number(jb)+Number(jc)
+Number(jd)+Number(je) +Number(jf));
document.getElementById("vj").value = Math.round(jx/6);
}
function Tom() {
let ta = document.getElementById("yt").value;
let tb = document.getElementById("st").value;
let tc = document.getElementById("et").value;
let td = document.getElementById("tt").value;
let te = document.getElementById("mt").value;
let tf = document.getElementById("at").value;
let tx = document.getElementById("ttt").value = (Number(ta) +Number(tb)+Number(tc)
+Number(td)+Number(te) +Number(tf));
document.getElementById("vt").value = Math.round(tx/6);
}