可编辑表格html+css+js

我是利用的json导入的数据,并可对相应的数据在表格中增删改

JSON

{
  "students": [
    {
      "id": 202301,
      "name": "张三",
      "english": 88,
      "math": 99,
      "chinese": 100
    },
    {
      "id": 202302,
      "name": "李四",
      "english": 73,
      "math": 69,
      "chinese": 90
    }
  ]
}

HTML(采取表头固定的方式)

<div class="warpper" style="text-align: center">
      <label for="studentName">姓名:</label>
      <input type="text" id="studentName" name="studentName" required />
      <label for="english">英语:</label>
      <input type="text" id="english" name="english" required />
      <label for="math">数学:</label>
      <input type="text" id="math" name="math" required />
      <label for="chinese">语文:</label>
      <input type="text" id="chinese" name="chinese" required />
      <button id="increase">新增</button>
      <table class="myTable" id="myTable">
        <thead>
          <th>学号</th>
          <th>姓名</th>
          <th>英语</th>
          <th>数学</th>
          <th>语文</th>
          <th>总分</th>
          <th>删除</th>
        </thead>
      </table>
    </div>

JS

const idReg = "^\\d{3}$";
    const nameReg = "^[\\u4e00-\\u9fa5]{2,4}$";
    const gradReg = "^([0-9]{1,2}|100)$";
    const myTable = document.getElementById("myTable");

    const instance = axios.create({
      baseURL: "http://localhost:3000/students",
      timeout: 10000,
    });

    const thead = myTable.querySelector("thead");
    console.log(thead);

    // 查询渲染信息
    function getData() {
      instance
        .get("")
        .then((res) => {
          const studentsData = res.data;
          console.log(res.data);
          let studentsList = "";
          for (let student of studentsData) {
            const rowData =
              `<tr>` +
              `<td contenteditable='true' data-reg='${idReg}'>${student.id}</td>` +
              `<td contenteditable='true' data-reg='${nameReg}'>${student.name}</td>` +
              `<td contenteditable='true' data-reg='${gradReg}'>${student.english}</td>` +
              `<td contenteditable='true' data-reg='${gradReg}'>${student.math}</td>` +
              `<td contenteditable='true' data-reg='${gradReg}'>${student.chinese}</td>` +
              `<td id="total">${
                student.chinese + student.math + student.english
              }</td>` +
              `<td><button  class='deleteBtn' id="${student.id}">删除</button></td>` +
              `</tr>`;
            studentsList += rowData;
          }
          myTable.innerHTML += studentsList;
          Delete();
        })
        .catch((error) => {
          console.log(error);
        });
    }
    getData();

    // 修改数据

    myTable.addEventListener(
      "click",
      function (e) {
        const td = e.target;
        const total = td.parentNode.childNodes[5];
        const reg = new RegExp(td.dataset.reg);
        if (td.tagName === "TD") {
          const text = td.innerHTML;
          td.innerHTML = "";
          const input = document.createElement("input");
          input.type = "text";
          input.style.border = "none";
          input.style.outline = "none";
          input.style.textAlign = "center";
          input.style.width = "110px";
          input.style.height = "48px";
          input.style.fontSize = "16px";
          input.value = text;
          td.appendChild(input);
          input.focus();
          input.addEventListener(
            "blur",
            (e) => {
              const input = e.target;
              const newValue = input.value;
              if (reg.test(newValue)) {
                td.innerHTML = newValue;
                total.innerHTML =
                  parseInt(total.innerHTML) +
                  parseInt(newValue) -
                  parseInt(text);
                instance.post;
              } else {
                alert("不符合");
                td.innerHTML = text;
              }
            },
            false
          );
        }
      },
      false
    );

    // 删除数据

    function Delete() {
      const delButtons = document.querySelectorAll(".deleteBtn");
      delButtons.forEach((one) => {
        one.addEventListener("click", function (event) {
          const id = event.target.getAttribute("id");
          instance
            .delete(`/${id}`)
            .then((res) => {
              console.log(res.data);
              getData();
            })
            .catch((err) => {
              console.error(err);
            });
        });
      });
    }

    //增加数据
    const increase = document.getElementById("increase");
    increase.addEventListener("click", (e) => {
      const studentName = document.getElementById("studentName").value;
      const english = document.getElementById("english").value;
      english > 0 && english <= 100 ? english : (english = 0);
      const math = document.getElementById("math").value;
      math > 0 && math <= 100 ? math : (math = 0);
      const chinese = document.getElementById("chinese").value;
      chinese > 0 && chinese <= 100 ? chinese : (chinese = 0);
      instance
        .post("/", {
          name: String(studentName),
          english: Number(english),
          math: Number(math),
          chinese: Number(chinese),
        })
        .then((res) => {
          console.log(res.data);
          getData();
        })
        .catch((err) => {
          console.error(err);
        });
    });

整体如下

<!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>
    <script src="axios.js"></script>
    <style>
      .warpper {
        position: relative;
        margin-top: 10%;
      }

      .myTable {
        table-layout: fixed;
        width: 0;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        border: 1px solid #000;
        border-collapse: collapse;
      }

      th {
        background-color: #ffd220;
      }

      input {
        background-color: greenyellow;
      }

      th,
      td {
        width: 120px;
        height: 50px;
        text-align: center;
        border: 1px solid #000;
        font-size: 16px;
      }
    </style>
  </head>

  <body>
    <div class="warpper" style="text-align: center">
      <label for="studentName">姓名:</label>
      <input type="text" id="studentName" name="studentName" required />
      <label for="english">英语:</label>
      <input type="text" id="english" name="english" required />
      <label for="math">数学:</label>
      <input type="text" id="math" name="math" required />
      <label for="chinese">语文:</label>
      <input type="text" id="chinese" name="chinese" required />
      <button id="increase">新增</button>
      <table class="myTable" id="myTable">
        <thead>
          <th>学号</th>
          <th>姓名</th>
          <th>英语</th>
          <th>数学</th>
          <th>语文</th>
          <th>总分</th>
          <th>删除</th>
        </thead>
      </table>
    </div>
  </body>
  <script>
    // 验证规则
    const idReg = "^\\d{3}$";
    const nameReg = "^[\\u4e00-\\u9fa5]{2,4}$";
    const gradReg = "^([0-9]{1,2}|100)$";
    const myTable = document.getElementById("myTable");

    const instance = axios.create({
      baseURL: "http://localhost:3000/students",
      timeout: 10000,
    });

    const thead = myTable.querySelector("thead");
    console.log(thead);

    // 查询渲染信息
    function getData() {
      instance
        .get("")
        .then((res) => {
          const studentsData = res.data;
          console.log(res.data);
          let studentsList = "";
          for (let student of studentsData) {
            const rowData =
              `<tr>` +
              `<td contenteditable='true' data-reg='${idReg}'>${student.id}</td>` +
              `<td contenteditable='true' data-reg='${nameReg}'>${student.name}</td>` +
              `<td contenteditable='true' data-reg='${gradReg}'>${student.english}</td>` +
              `<td contenteditable='true' data-reg='${gradReg}'>${student.math}</td>` +
              `<td contenteditable='true' data-reg='${gradReg}'>${student.chinese}</td>` +
              `<td id="total">${
                student.chinese + student.math + student.english
              }</td>` +
              `<td><button  class='deleteBtn' id="${student.id}">删除</button></td>` +
              `</tr>`;
            studentsList += rowData;
          }
          myTable.innerHTML += studentsList;
          Delete();
        })
        .catch((error) => {
          console.log(error);
        });
    }
    getData();

    // 修改数据

    myTable.addEventListener(
      "click",
      function (e) {
        const td = e.target;
        const total = td.parentNode.childNodes[5];
        const reg = new RegExp(td.dataset.reg);
        if (td.tagName === "TD") {
          const text = td.innerHTML;
          td.innerHTML = "";
          const input = document.createElement("input");
          input.type = "text";
          input.style.border = "none";
          input.style.outline = "none";
          input.style.textAlign = "center";
          input.style.width = "110px";
          input.style.height = "48px";
          input.style.fontSize = "16px";
          input.value = text;
          td.appendChild(input);
          input.focus();
          input.addEventListener(
            "blur",
            (e) => {
              const input = e.target;
              const newValue = input.value;
              if (reg.test(newValue)) {
                td.innerHTML = newValue;
                total.innerHTML =
                  parseInt(total.innerHTML) +
                  parseInt(newValue) -
                  parseInt(text);
                instance.post;
              } else {
                alert("不符合");
                td.innerHTML = text;
              }
            },
            false
          );
        }
      },
      false
    );

    // 删除数据

    function Delete() {
      const delButtons = document.querySelectorAll(".deleteBtn");
      delButtons.forEach((one) => {
        one.addEventListener("click", function (event) {
          const id = event.target.getAttribute("id");
          instance
            .delete(`/${id}`)
            .then((res) => {
              console.log(res.data);
              getData();
            })
            .catch((err) => {
              console.error(err);
            });
        });
      });
    }

    //增加数据
    const increase = document.getElementById("increase");
    increase.addEventListener("click", (e) => {
      const studentName = document.getElementById("studentName").value;
      const english = document.getElementById("english").value;
      english > 0 && english <= 100 ? english : (english = 0);
      const math = document.getElementById("math").value;
      math > 0 && math <= 100 ? math : (math = 0);
      const chinese = document.getElementById("chinese").value;
      chinese > 0 && chinese <= 100 ? chinese : (chinese = 0);
      instance
        .post("/", {
          name: String(studentName),
          english: Number(english),
          math: Number(math),
          chinese: Number(chinese),
        })
        .then((res) => {
          console.log(res.data);
          getData();
        })
        .catch((err) => {
          console.error(err);
        });
    });
  </script>
</html>

实现效果

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 这8套大气漂亮的HTML CSS网站模板和网页设计源码,都是基于最新的Web前端技术和趋势,兼容多种浏览器并具有良好的响应式布局。这些模板可供自由发挥,可以用于众多领域,比如企业、商店、博客、摄影集、音乐之类的。 这些模板还配有全面的文档说明,以帮助开发人员更轻松地使用、扩展和修改各种功能。以下是这些模板的一些特点: 1. 采用最新的HTML5和CSS3技术,让你的网站更快,响应更好,同时具有更佳的可访问性。 2. 多种响应式布局,可在不同的屏幕大小和设备上呈现最佳体验。 3. 经过优化的代码和资源,减少了加载时间和带宽要求。 4. 具有多个预定义的颜色和样式主题,方便快捷地实现不同的设计想法。 5. 集成了多个实用的JavaScript插件和工具,如轮播图、图像滤镜、表格排序、表单验证等等。 总之,这8套HTML CSS网站模板是完美的选择,无论您是建立企业网站、博客、在线商店还是个人品牌,这些模板都能满足您的需求,并帮助您快速、轻松地实现您的目标。 ### 回答2: 在现代互联网时代,网站设计已经成为了商业形象和用户体验的重要组成部分。因此,为了使网站能够达到最佳效果,我们常常需要使用一些高质量的网站模板和设计源码。下面我将介绍8款大气漂亮的HTML/CSS网站模板和网页设计源码。 1. Shakuro(https://www.behance.net/gallery/47635119/Shakuro-Free-HTML-Template):Shakuro是一个充满活力和现代感的HTML/CSS模板。它为各种各样的网站提供了完美的基础。 2. Start(https://www.behance.net/gallery/83485165/Start-Bootstrap-Landing-Page-Freebie):Start是一个出色的Bootstrap网站模板,它拥有高质量的设计元素和响应式设计。 3. Liquid(https://www.behance.net/gallery/82156901/Liquid-Responsive-Web-Template):Liquid是一个具有生动色彩和创意风格的HTML/CSS模板。它的设计极具创意,并能够适应多种不同的设计需求。 4. Space(https://www.behance.net/gallery/76018889/Free-Space-HTML5-Template):Space是一个基于HTML5的高质量响应式设计模板。它可以为您的网站带来出色的视觉效果。 5. Mosaic(https://www.behance.net/gallery/78824451/Mosaic-Website-Freebie):Mosaic是一个充满活力的设计模板,它适用于任何类型的网站。使用这个模板,您可以轻松地创建一个让用户惊艳的设计。 6. Ameixa(https://www.behance.net/gallery/103353015/Ameixa-Landing-Page-Freebie):Ameixa是一个出色的HTML/CSS模板,它的视觉效果非常棒。各种设计元素的运用,让您的网站在视觉上更加出色。 7. Synergy(https://www.behance.net/gallery/76063147/Synergy-Free-Web-Template):Synergy是一个出色的HTML/CSS响应式设计模板。它的设计极具现代感,并且非常适合用于企业、创意等各种类型的网站。 8. Success(https://www.behance.net/gallery/69885913/Freebie-PSD-Success-Landing-Page):Success是一个出色的PSD源码设计,它适用于任何类型的网站。它的设计风格稳重而不失现代感,让您的网站更加优秀。 总之,这8款大气漂亮的HTML/CSS网站模板和网页设计源码为您的网站提供了丰富多彩的设计选项,帮助您实现更好的商业效果和用户体验,是不可多得的设计资源。 ### 回答3: 这是一个非常好的想法,因为现代社会中,网站已经成为人们获取信息、商品和服务的主要途径之一。如果你想开始一个网站或升级你现有的网站,那么一个漂亮、独特和易于浏览的网页设计是至关重要的。 这8套大气漂亮的HTML CSS网站模板和网页设计源码,提供了一个很好的起点,可以让你更快地设计一个华丽的网站。它们被设计成多用途的,涵盖了各种类型的业务,包括旅游、餐饮、商务、技术、艺术和新闻,以帮助你快速搭建一个具有响应式设计风格的网站。 这些模板和源码的特点包括: - 灵活的布局和可定制的风格 - 响应式设计、适应不同屏幕尺寸 - 丰富的页面元素,如导航菜单、轮播图、图片库等 - 内置的表单和电子邮件订阅功能 - SEO友好性,包括谷歌分析和元标签 - 可以轻松编辑和更新 使用这些大气漂亮的HTML CSS网站模板和网页设计源码,可以帮助你设计一个印象深刻的网站,来促进你的业务增长,提升你的品牌形象,吸引更多潜在客户,并增强你的网站的可发现性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值