亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery實現表格的增、刪、改操作示例

 更新時間:2019年01月27日 11:45:09   作者:貓屎不是咖啡  
這篇文章主要介紹了jQuery實現表格的增、刪、改操作,涉及基于jQuery的事件響應及頁面元素動態(tài)操作相關實現技巧,需要的朋友可以參考下

本文實例講述了jQuery實現表格的增、刪、改操作。分享給大家供大家參考,具體如下:

這里實現的是在jQuery中通過按鈕的形式,對表格進行的一些基本操作,可以實現表格的增刪改操作,并實現對鼠標事件監(jiān)聽,實現表格的高亮行操作。

<head>
  <meta charset="UTF-8">
  <title>chabaoo.cn 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>

效果圖如下:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。

更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery操作xml技巧總結》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結》、《jQuery擴展技巧總結》及《jquery選擇器用法總結

希望本文所述對大家jQuery程序設計有所幫助。

相關文章

最新評論