jQuery實現表格的增、刪、改操作示例
本文實例講述了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程序設計有所幫助。
- jQuery動態(tài)操作表單示例【基于table表格】
- jQuery+Datatables實現表格批量刪除功能【推薦】
- 基于jquery的實現簡單的表格中增加或刪除下一行
- jQuery實現表格行和列的動態(tài)添加與刪除方法【測試可用】
- jQuery對table表格進行增刪改查
- 基于jQuery的動態(tài)增刪改查表格信息,可左鍵/右鍵提示(原創(chuàng)自Zjmainstay)
- jquery動態(tài)增加刪除表格行的小例子
- 基于jQuery實現表格的查看修改刪除
- JQuery動態(tài)添加和刪除表格行的方法
- 原生JS和JQuery動態(tài)添加、刪除表格行的方法
- jquery動態(tài)增加刪減表格行特效
相關文章
jquery imgareaselect 使用利用js與程序結合實現圖片剪切
當前在ff3下,用jquery的 width()與height()函數,在不設置圖片的寬度與高度的時候,不能取到 需要在圖片load函數里面初始化才可以2009-07-07