基于javascript实现表格的简单操作

作者:朱鹏-天羽 时间:2024-04-30 08:55:24 

本文实例为大家分享了js表格操作的简单方法,供大家参考,具体内容如下


<!DOCTYPE html>
<html>
 <head>
   <meta charset="utf-8" />
   <title>zzzz</title>
   <style>
     *{
       margin: 0;
       padding: 0;
     }
     body{
       width: 1000px;
       margin: 100px auto;
       font-family: "微软雅黑";
       font-size: 18px;
       background-color: #fff;
     }
     #table tr{
       text-align: center;
     }
     tbody tr:nth-child(2n+1){
       background-color: #ccc;
     }
     tbody tr:hover{
       background-color: green;
     }
     input[type=text]{
       height: 25px;
       width: 136px;
       background-color: turquoise;
       margin-bottom: 10px;
     }
   </style>
   <script type="text/javascript">
     window.onload=function(){
       var otb=document.getElementById("table");
       var otr=otb.tBodies[0].rows;
       var oadd=document.getElementById("add");
       var oname=document.getElementById('name');
       var osex=document.getElementById('sex');
       var id=otr.length+1;
       oadd.onclick=function(){
         var atr=document.createElement('tr');
         var atd1=document.createElement('td');
         var atd2=document.createElement('td');
         var atd3=document.createElement("td");
         var atd4=document.createElement("td");
         atd4.innerHTML='<a>删除</a>';
         atd1.innerHTML=id++;
         atd2.innerHTML=oname.value;
         atd3.innerHTML=osex.value;

atr.appendChild(atd1);
         atr.appendChild(atd2);
         atr.appendChild(atd3);
         atr.appendChild(atd4)
         otb.tBodies[0].appendChild(atr);

atd4.getElementsByTagName('a')[0].onclick=function(){
           otb.tBodies[0].removeChild(this.parentNode.parentNode)
       }

}

var obtn=document.getElementById("btn");
     var otxt=document.getElementById("ss");    
     obtn.onclick=function(){
       for(var i=0;i<otb.tBodies[0].rows.length;i++){
         var osta=otb.tBodies[0].rows[i].cells[1].innerHTML.toLowerCase();
         var ot=otxt.value.toLowerCase();
         var oar=ot.split(' ');
         otb.tBodies[0].rows[i].style.background='';
         for(var j=0;j<oar.length;j++){
         if(osta.search(oar[j])!=-1){
           otb.tBodies[0].rows[i].style.background='red';
         }
       }

}
     }
   }
   </script>
 </head>
 <body>
   <input type="text" placeholder="请输入搜索内容" id="ss"/>
   <input type="button" value="查询" id="btn"/><br />
   姓名:<input type="text" id="name" />
   性别:<input type="text" id="sex" />
   <input type="button" value="添加" id="add"/>
   <table border="1" width="400px" id="table">
     <thead>
       <tr>
         <td>序号</td>
         <td>人名</td>
         <td>性别</td>
         <td>修改</td>
       </tr>
       <tbody>
         <tr>
         <td>1</td>
         <td>张三</td>
         <td>男</td>
         <td>删除</td>
       </tr>
       <tr>
         <td>2</td>
         <td>李四</td>
         <td>男</td>
         <td>删除</td>
       </tr>
       <tr>
         <td>3</td>
         <td>Caesar</td>
         <td>女</td>
         <td>删除</td>
       </tr>
       <tr>
         <td>4</td>
         <td>刘言</td>
         <td>女</td>
         <td>删除</td>
       </tr>
       </tbody>
     </thead>
   </table>
 </body>
</html>
标签:js,表格
0
投稿

猜你喜欢

  • python实现简单多人聊天室

    2022-08-17 15:04:13
  • php中debug_backtrace、debug_print_backtrace和匿名函数用法实例

    2023-06-11 23:08:24
  • Python 解决logging功能使用过程中遇到的一个问题

    2023-05-25 11:31:05
  • python通过re正则表达式切割中英文的操作

    2021-11-29 04:41:23
  • python Selenium等待元素出现的具体方法

    2022-02-22 02:47:20
  • 如何通过python实现IOU计算代码实例

    2021-07-09 02:06:44
  • python 包之 multiprocessing 多进程

    2021-02-25 12:13:15
  • python发布模块的步骤分享

    2023-08-07 11:29:25
  • python颜色随机生成器的实例代码

    2022-12-03 19:40:05
  • asp函数收藏

    2009-04-21 13:10:00
  • keras打印loss对权重的导数方式

    2023-05-17 18:21:11
  • 快速解决百度编译器json报错的问题

    2023-04-08 16:20:17
  • python人工智能tensorflow函数tensorboard使用方法

    2021-04-21 14:52:46
  • python装饰器初探(推荐)

    2023-01-19 14:40:27
  • Request的中断和ErrorHandler实例解析

    2021-11-25 18:42:04
  • mysql实现事务的提交和回滚实例

    2024-01-23 04:13:12
  • MySQL备份时排除指定数据库的方法

    2024-01-19 03:59:21
  • 谦逊编程(翻译整理)

    2009-07-28 12:52:00
  • 关于textarea的直观换行

    2010-03-18 15:59:00
  • 微信跳一跳python辅助软件思路及图像识别源码解析

    2023-01-10 22:45:57
  • asp之家 网络编程 m.aspxhome.com