萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> jQuery實現表格行上移下移和置頂的方法

jQuery實現表格行上移下移和置頂的方法

   本文實例講述了jQuery實現表格行上移下移和置頂的方法。分享給大家供大家參考。具體實現方法如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <title>jQuery實現表格行上移下移和置頂</title> <meta name="keywords" content="jquery,滑動" /> <meta name="description" content="Helloweba文章結合實例演示HTML5、CSS3、jquery、PHP等WEB技術應用。" /> <link rel="stylesheet" type="text/css" href="css/main.css" /> <style type="text/css"> .demo{width:600px; margin:60px auto 10px auto; font-size:16px} .table {border-collapse: collapse !important;width: 100%;max-width: 100%;margin-bottom: 20px;} .table td,.table th {background-color: #fff !important;} .table-bordered th,.table-bordered td {border: 1px solid #ddd !important;} .table tr td {padding: 8px;line-height: 1.42857143;vertical-align: middle;border-bottom: 1px solid #ddd;} .table tr:hover {background-color: #f5f5f5;} </style> <script type="text/javascript" src="js/jquery.min.js"></script> <script> $(function(){ //上移 var $up = $(".up") $up.click(function() { var $tr = $(this).parents("tr"); if ($tr.index() != 0) { $tr.fadeOut().fadeIn(); $tr.prev().before($tr); } }); //下移 var $down = $(".down"); var len = $down.length; $down.click(function() { var $tr = $(this).parents("tr"); if ($tr.index() != len - 1) { $tr.fadeOut().fadeIn(); $tr.next().after($tr); } }); //置頂 var $top = $(".top"); $top.click(function(){ var $tr = $(this).parents("tr"); $tr.fadeOut().fadeIn(); $(".table").prepend($tr); $tr.css("color","#f60"); }); }); </script> </head> <body> <div id="main"> <div class="demo"> <table class="table"> <tr> <td>HTML5獲取地理位置定位信息</td> <td>2015-04-25</td> <td><a href="#" class="up">上移</a> <a href="#" class="down">下移</a> <a href="#" class="top">置頂</a></td> </tr> <tr> <td>CSS+Cookie實現的固定頁腳廣告條</td> <td>2015-04-18</td> <td><a href="#" class="up">上移</a> <a href="#" class="down">下移</a> <a href="#" class="top">置頂</a></td> </tr> <tr> <td>純CSS3制作漂亮的價格表</td> <td>2015-04-10</td> <td><a href="#" class="up">上移</a> <a href="#" class="down">下移</a> <a href="#" class="top">置頂</a></td> </tr> <tr> <td>jQuery實現的測試答題功能</td> <td>2015-03-29</td> <td><a href="#" class="up">上移</a> <a href="#" class="down">下移</a> <a href="#" class="top">置頂</a></td> </tr> </table> </div> </div> </body> </html>

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

copyright © 萬盛學電腦網 all rights reserved