首页 > 语言 > JavaScript > 正文

jQuery实现表格行上移下移和置顶的方法

2024-05-06 16:20:46
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了jQuery实现表格行上移下移和置顶的方法,涉及jQuery操作页面元素的相关技巧,需要的朋友可以参考下

本文实例讲述了jQuery实现表格行上移下移和置顶的方法。分享给大家供大家参考。具体实现方法如下:

 

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

希望本文所述对大家的jQuery程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选