怎么让td里的内容置顶_jQuery实现表格行上移下移和置顶的方法

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

.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;}

$(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");

});

});

HTML5获取地理位置定位信息2015-04-25上移 下移 置顶
CSS+Cookie实现的固定页脚广告条2015-04-18上移 下移 置顶
纯CSS3制作漂亮的价格表2015-04-10上移 下移 置顶
jQuery实现的测试答题功能2015-03-29上移 下移 置顶
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个操作可以通过 JavaScript 代码来实现。首先,你需要在表格中给每一的“上移”和“下移”按钮绑定点击事件。在点击事件处理函数中,你可以获取当前的索引,然后通过 DOM 操作来改变其在表格中的位置。 以下是一个简单示例代码,你可以参考它来实现你的功能: ```html <table id="myTable"> <tr> <td>Row 1</td> <td><button class="up">↑</button></td> <td><button class="down">↓</button></td> </tr> <tr> <td>Row 2</td> <td><button class="up">↑</button></td> <td><button class="down">↓</button></td> </tr> <tr> <td>Row 3</td> <td><button class="up">↑</button></td> <td><button class="down">↓</button></td> </tr> </table> <script> // 获取所有的上移下移按钮 const upButtons = document.querySelectorAll('.up'); const downButtons = document.querySelectorAll('.down'); // 给每个按钮绑定点击事件 upButtons.forEach(button => { button.addEventListener('click', () => { const row = button.parentNode.parentNode; // 获取当前 const prevRow = row.previousElementSibling; // 获取前一 if (prevRow) { // 如果前一存在 row.parentNode.insertBefore(row, prevRow); // 将当前插入到前一之前 } }); }); downButtons.forEach(button => { button.addEventListener('click', () => { const row = button.parentNode.parentNode; // 获取当前 const nextRow = row.nextElementSibling; // 获取后一 if (nextRow) { // 如果后一存在 row.parentNode.insertBefore(nextRow, row); // 将后一插入到当前之前 } }); }); </script> ``` 这段代码会为表格中的每个“上移”和“下移”按钮绑定点击事件,并在点击时移动其所在。注意,这只是一个示例代码,你需要根据你的具体需求来修改它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值