jQuery 删除一个元素并对剩余元素重新编号
有谁看到我的代码中的缺陷吗,因为这让我难住了!
function removeMLRow(rowNo) {
$('#ml_organize li:eq(' + (rowNo - 1) + ')').remove();
$($('#ml_organize li:eq(' + (rowNo) + ')').get().reverse()).each(function() {
var newID = 'li' + ($(this).index() - 1);
$(this).attr('id',newID);
});
}
Does anyone see the flaw in my code here, because this one has me stumped!
function removeMLRow(rowNo) {
$('#ml_organize li:eq(' + (rowNo - 1) + ')').remove();
$($('#ml_organize li:eq(' + (rowNo) + ')').get().reverse()).each(function() {
var newID = 'li' + ($(this).index() - 1);
$(this).attr('id',newID);
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不能根据问题确定,但我认为这就是您所追求的:
首先,您可以使用
.eq()
而不是:eq()
只是让事情变得更干净。然后我们使用.slice()
来获取所有 < code>:gt()
(大于 - index),但.slice()
只是修剪字符串连接(虽然速度更快,但差异无穷小)。I can't say for sure based on the question, but I think this is what you're after:
First, you can use
.eq()
instead of:eq()
to just make things cleaner. Then we're using.slice()
to get all the<li>
elements after the one we removed and are numbering only those<li>
's. You could use:gt()
(greater-than-index), but.slice()
just trims down on the string concatenation (and is a bit faster, infinitesimal difference though).您确定应该使用反向吗?据我所知,您正在删除一个元素,然后重新编号回到顶部。你应该重新编号到底部还是数字颠倒过来?
更多信息请@dave
Are you sure you should be using reverse. from what i see you're removing an element and then renumbering back to the top. should you be renumbering to the bottom or are the numbers reversed?
more info please @dave
尼克,你就快到了! newID 中需要 (+1) 而不是 (-1)。
感谢大家的帮助!
Nick, you were ALMOST there! Needed to (+1) instead of (-1) in the newID.
Thanks to all for your help!