
HTML
页面上是一个简单的数据表格,我们在数据行中分别放置“上移”,“下移”和“置顶”三个链接,并且分别定义三个class属性,我们来通过jQuery实现这些操作。
<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实现的固定页脚广告条置顶</a></td></tr>... </table>jQuery
$(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");}); }); 当然,实际应用中应该结合您的项目,在操作“上移”,“下移”和“置顶”完成时,应该和后台程序进行Ajax异步交互,保证排序数据真正被后台记录,然后刷新后会展示新的排序结果,本文不再对该异步操作做详细解说。