黑马前端——days20_API

页面框架文件

<!DOCTYPE html>
<html lang="zh-CN">

<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>Document</title>
  <link rel="stylesheet" href="style.css" />
</head>

<body>
  <h1>新增学员</h1>
  <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>
      <!-- 数据内容 -->
    </tbody>
  </table>
  <script src="index.js"></script>
</body>

</html>

pic1
pic2
pic3

格式文件

* {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color: #721c24;
}

h1 {
  text-align: center;
  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;
}

事件文件

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')
const items = document.querySelectorAll('[name]')
const arr = []
let id = 0

// 1. 录入模块
const info = document.querySelector('.info')
info.addEventListener('submit', function (e) {
  e.preventDefault()

  for (let i = 0; i < items.length; i++) {
    if (items[i].value === '') {
      return alert('输入内容不能为空')
    }
  }

  const obj = {
    stuId: id + 1,
    uname: uname.value,
    age: age.value,
    gender: gender.value,
    salary: salary.value,
    city: city.value
  }

  arr.push(obj)
  id += 1
  this.reset()
  render()
})


// 2. 渲染函数
function render() {
  tbody.innerHTML = ''

  for (let i = 0; i < arr.length; i++) {
    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="#" data-id=${i}>删除</a>
      </td>
    `
    tbody.appendChild(tr)
  }
}

// 3. 删除操作
tbody.addEventListener('click', function (e) {
  if (e.target.tagName === 'A') {
    arr.splice(e.target.dataset.id, 1)
    console.log(arr)
    render()
  }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胆怯与勇敢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值