欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實(shí)現(xiàn)表格的增、刪、改操作示例

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

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

這里實(shí)現(xiàn)的是在jQuery中通過(guò)按鈕的形式,對(duì)表格進(jìn)行的一些基本操作,可以實(shí)現(xiàn)表格的增刪改操作,并實(shí)現(xiàn)對(duì)鼠標(biāo)事件監(jiān)聽(tīng),實(shí)現(xiàn)表格的高亮行操作。

<head>
  <meta charset="UTF-8">
  <title>www.dbjr.com.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();
        });
      });
      //設(shè)置高亮行
      $("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代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。

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

希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論