首页 > 语言 > JavaScript > 正文

jQuery实现表格的增、删、改操作示例

2024-05-06 15:42:53
字体:
来源:转载
供稿:网友

本文实例讲述了jQuery实现表格的增、删、改操作。分享给大家供大家参考,具体如下:

这里实现的是在jQuery中通过按钮的形式,对表格进行的一些基本操作,可以实现表格的增删改操作,并实现对鼠标事件监听,实现表格的高亮行操作。

<head>  <meta charset="UTF-8">  <title>www.Vevb.com jQuery表格操作</title>  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>  <script type="text/javascript">    $(document).ready(function() {      //添加一行      $("#one").click(function() {        var $td = $("#trOne").clone();        $("table").append($td);        $("table tr:last").find("input").val("");      });      //删除一行      $("#two").click(function() {        $("table tr:not(:first):last").remove();      });      //删除所有行      $("#three").click(function() {        /*var len=$("tr").length;        for(var i=0;i<=len;i++){          $("tr")[i].remove();        }*/        //除第一行为其它行删除        $("tr:not(:first)").remove();      });      //删除选中的行      $("#four").click(function() {        //遍历选中的checkbox        $("[type='checkbox']:checked").each(function() {          //获取checkbox所在行的顺序          n = $(this).parents("tr").index();          $("table").find("tr:eq(" + n + ")").remove();        });      });      //设置高亮行      $("tr").mouseover(function() {        $(this).css("background-color","red");      });      $("tr").mouseout(function(){        $(this).css("background-color","white");      });    });  </script></head><body>  <input type="button" id="one" value="添加一行" /><br />  <input type="button" id="two" value="删除一行" /><br />  <input type="button" id="three" value="删除所有行" /><br />  <input type="button" id="four" value="删除选中的行" /><br />  <table width="400px" height="50px" border="2px" cellspacing="0" cellpadding="0">    <tr id="trOne">      <td><input type="checkbox" name=""></td>      <td><input type="" name="" value="姓名" </td>        <td><input type="" name="" value="年龄" </td>          <td><input type="" name="" value="性别" </td>    </tr>    <tr>      <td><input type="checkbox" name=""></td>      <td><input type="" name="" value="张三" </td>        <td><input type="" name="" value="18" </td>          <td><input type="" name="" value="男" </td>    </tr>    <tr>      <td><input type="checkbox" name=""></td>      <td><input type="" name="" value="李四" </td>        <td><input type="" name="" value="18" </td>          <td><input type="" name="" value="男" </td>    </tr>    <tr>      <td><input type="checkbox" name=""></td>      <td><input type="" name="" value="王五" </td>        <td><input type="" name="" value="18" </td>          <td><input type="" name="" value="男" </td>    </tr>  </table></body>            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选