首页 > 其他分享 >JS--demo2录入学生信息

JS--demo2录入学生信息

时间:2024-04-08 16:59:45浏览次数:13  
标签:info arr const -- tr tbody JS demo2 document

实现学生信息录取。

效果图:
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>学生信息管理</title>
  <link rel="stylesheet" href="css/index.css" />
</head>

<body>
  <h1>新增学员</h1>
<!--  form 表单-->
  <form class="info" autocomplete="off">
    姓名:<input type="text" class="uname" name="uname" />
    年龄:<input type="text" class="age" name="age" />
    性别:
    <select name="gender" class="gender">
      <option value="男">男</option>
      <option value="女">女</option>
    </select>
    薪资:<input type="text" class="salary" name="salary" />
    就业城市:<select name="city" class="city">
      <option value="北京">北京</option>
      <option value="上海">上海</option>
      <option value="广州">广州</option>
      <option value="深圳">深圳</option>
      <option value="曹县">曹县</option>
    </select>
    <button class="add">录入</button>
  </form>

  <h1>就业榜</h1>
  <table>
    <thead>
      <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>性别</th>
        <th>薪资</th>
        <th>就业城市</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
      <!--
        <tr>
          <td>1001</td>
          <td>欧阳霸天</td>
          <td>19</td>
          <td>男</td>
          <td>15000</td>
          <td>上海</td>
          <td>
            <a href="javascript:">删除</a>
          </td>
        </tr>
        -->
    </tbody>
  </table>
  <script>
    // 获取元素
    const uname = document.querySelector('.uname')
    const age = document.querySelector('.age')
    const gender = document.querySelector('.gender')
    const salary = document.querySelector('.salary')
    const city = document.querySelector('.city')
    const tbody = document.querySelector('tbody')
    // 获取所有带有name属性的 元素
    const items = document.querySelectorAll('[name]')
    // 声明一个空的数组, 增加和删除都是对这个数组进行操作
    const arr = []

    // 1. 录入模块
    // 1.1 表单提交事件
    const info = document.querySelector('.info')
    info.addEventListener('submit', function (e) {  //提交表单事件
      // 阻止默认行为  不跳转
      e.preventDefault()
      // console.log(11)

      // 这里进行表单验证  如果不通过,直接中断,不需要添加数据
      // 先遍历循环
      for (let i = 0; i < items.length; i++) {
        if (items[i].value === '') {
          return alert('输入内容不能为空')
        }
      }
      // 创建新的对象
      const obj = {
        stuId: arr.length + 1,
        uname: uname.value,
        age: age.value,
        gender: gender.value,
        salary: salary.value,
        city: city.value
      }
      // console.log(obj)
      // 追加给数组里面
      arr.push(obj)
      // console.log(arr)
      // 清空表单  重置
      this.reset()
      // 调用渲染函数
      render()
    })


    // 2. 渲染函数 因为增加和删除都需要渲染
    function render() {
      // 先清空tbody 以前的行 ,把最新数组里面的数据渲染完毕
      tbody.innerHTML = ''
      // 遍历arr数组
      for (let i = 0; i < arr.length; i++) {
        // 生成 tr
        const tr = document.createElement('tr')
        tr.innerHTML = `
          <td>${arr[i].stuId}</td>
          <td>${arr[i].uname}</td>
          <td>${arr[i].age}</td>
          <td>${arr[i].gender}</td>
          <td>${arr[i].salary}</td>
          <td>${arr[i].city}</td>
          <td>
            <a href="javascript:" data-id=${i}>删除</a>
          </td>
        `
        // 追加元素  父元素.appendChild(子元素)
        tbody.appendChild(tr)
      }
    }


    // 3. 删除操作
    // 3.1 事件委托 tbody
    tbody.addEventListener('click', function (e) {
      if (e.target.tagName === 'A') {
        // 得到当前元素的自定义属性 data-id
        // console.log(e.target.dataset.id)
        // 删除arr 数组里面对应的数据
        arr.splice(e.target.dataset.id, 1)
        console.log(arr)
        // 从新渲染一次
        render()
      }
    })
  </script>

</body>

</html>

css文件是外部引入的。

* {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color:#721c24;
}
h1 {
  text-align: center;
  /*color:#333;*/
  color:#333;
  margin: 20px 0;

}
table {
  margin:0 auto;
  width: 800px;
  border-collapse: collapse;
  color:#004085;
}
th {
  padding: 10px;
  background: #cfe5ff;

  font-size: 20px;
  font-weight: 400;
}
td,th {
  border:1px solid #b8daff;
}
td {
  padding:10px;
  color:#666;
  text-align: center;
  font-size: 16px;
}
tbody tr {
  background: #fff;
}
tbody tr:hover {
  background: #e1ecf8;
}
.info {
  width: 900px;
  margin: 50px auto;
  text-align: center;
}
.info  input, .info select {
  width: 80px;
  height: 27px;
  outline: none;
  border-radius: 5px;
  border:1px solid #b8daff;
  padding-left: 5px;
  box-sizing: border-box;
  margin-right: 15px;
}
.info button {
  width: 60px;
  height: 27px;
  background-color: #004085;
  outline: none;
  border: 0;
  color: #fff;
  cursor: pointer;
  border-radius: 5px;
}
.info .age {
  width: 50px;
}

标签:info,arr,const,--,tr,tbody,JS,demo2,document
From: https://blog.csdn.net/ngczx/article/details/137510183

相关文章

  • Rust的变量类型__Data type
    EveryvalueinRustisofacertain datatype,whichtellsRustwhatkindofdataisbeingspecifiedsoitknowshowtoworkwiththatdata.We’lllookattwodatatypesubsets:scalarandcompound.在Rust中每一个值都有确定的变量类型,以告知Rust使用的数据是哪......
  • Linux基本命令入门详解
    Linux基本命令是Linux系统操作的基础,掌握这些命令对于初学者来说至关重要。下面将详细介绍一些常用的Linux基本命令,并附上实际例子。一、目录操作命令pwd:显示当前所在的目录路径。例子:在终端中输入pwd,将显示当前用户所在的目录路径,如/home/user。cd:切换目录。例子:输......
  • Linux命令之lldptool命令
    LLDP是一个数据链路层发现协议,LLDP协议使得接入网络的一台设备可以将其主要的能力,管理地址,设备标识,接口标识等信息发送给接入同一个局域网络的其它设备。lldptool工具采用的是LLDP协议,一般我们使用lldptool是为了得到设备的物理拓扑结构以及管理配置信息,比如说,和eth1网口相连的网......
  • 苍穹外卖10(Spring Task定时任务,WebSocket双向通信,订单状态定时处理,来电提醒,客户催单)
    目录一、SpringTask1.介绍2.入门1使用步骤2使用示例3.详解1@Scheduled注解2cron表达式1cron表达式6个域2各个域的取值说明4.小结二、订单状态定时处理1.需求分析1问题分析2功能需求2.代码开发1修改引导类加@EnableScheduling2创建OrderTask......
  • Linux之网络排错
    Linux网卡收包流程如下网卡收到数据包将数据包从网卡硬件缓存移动到服务器内存中(DMA方式,不经过CPU)通过硬中断通知CPU处理CPU通过软中断通知内核处理经过TCP/IP协议栈处理应用程序通过read()从socketbuffer读取数据网卡丢包我们先看下ifconfig的输出:#ifconfigeth......
  • 我要点名一款十字线上 PVP 游戏 - 1951
    \(1900-12=1888\)。怎么rating还是这么好笑。感觉每回打cf都要破防是怎么回事?被诈骗不还是因为菜?交\(12\)发不知道自己是怎么想的。然后E也不难,但是太晚了打不动了。下次交代码之前能不能拜托先把hack测一下?占了将近一半的RE哪个不是因为没开longlong?A01字符串......
  • 干货教程【软件篇】| PDF转换word工具永久免费使用
    给大家分享一个好用的PDF转换word的工具,完全免费、离线使用、且保存下来永久好用的工具。ps:本文只做好用的工具分享,不涉及任何工具的开发,感谢工具的开发者!关注文章下方公众号回复关键词【ptow】即可免费获取本工具。大家下载好之后就会进入安装界面,安装过程十分顺畅这里......
  • Vue.nextTick() 使用场景及实现原理
    Vue.nextTick()基本使用作用:等待下一次DOM更新刷新的工具方法。为什么需要用到Vue.nextTick()?当你在Vue中更改响应式状态时,最终的DOM更新并不是同步生效的,而是由Vue将它们缓存在一个队列中,直到下一个“tick”才一起执行。这样是为了确保每个组件无论发生多少......
  • MySql添加用户
    添加MySQL用户通常涉及创建一个新用户并为其分配相应的权限。以下是在MySQL中添加用户的一般步骤:连接到MySQL数据库服务器:mysql-uroot-p创建一个新用户并分配密码:CREATEUSER'new_user'@'localhost'IDENTIFIEDBY'password';请将'new_user'替换为新用户......
  • 汇编语言程序设计实验五 条件转移指令
    实验目的和要求(1)     编写实验任务要求的两个程序。(2)     写出调试以上程序,即修改程序参数,检查结果的操作方法。(3)     熟悉源程序汇编、连接命令的使用方法即要回答的内容。实验环境DOSBOX实验内容与过程验证以下程序16进制数化ASCII码的一般......