Acholi 2022-10-21 12:28 采纳率: 83.3%
浏览 45
已结题

求一个历遍数组的方法

用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);
}

img

基础代码


    <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);
}

  • 写回答

1条回答 默认 最新

  • 心寒丶 全栈领域优质创作者 2022-10-21 12:37
    关注

    你把这些需要加起来的列放一个公共的属性,比如第一列name都为 jack,第二列name都为tom,然后定义一个函数根据传进来的name去获取元素,再把获取的元素遍历相加一下就可以了吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 10月29日
  • 已采纳回答 10月21日
  • 修改了问题 10月21日
  • 修改了问题 10月21日
  • 展开全部

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。