本文實例講述了jQuery實現(xiàn)表格的增、刪、改操作。分享給大家供大家參考,具體如下:
這里實現(xiàn)的是在jQuery中通過按鈕的形式,對表格進行的一些基本操作,可以實現(xiàn)表格的增刪改操作,并實現(xiàn)對鼠標事件監(jiān)聽,實現(xiàn)表格的高亮行操作。
<head> <meta charset="UTF-8"> <title>www.gxlcms.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>
效果圖如下:
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關于jQuery相關內(nèi)容感興趣的讀者可查看本站專題:《jQuery表格(table)操作技巧匯總》、《jQuery操作xml技巧總結(jié)》、《jQuery form操作技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery擴展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設計有所幫助。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com