删除索引处的单个孩子 [英] Remove single child at index
问题描述
有一些示例可在给定索引后分离所有子级(例如此一),但不能仅删除其中一项.
There's examples to detach all children after a given index (like this one), but nothing to just remove one.
我尝试过的事情(显然是错误的):
Things I have tried (which are obviously incorrect):
this.element.find('.nav-tabs').children().splice(index, 1).remove();
this.element.find('.nav-tabs').children().slice(index).detach();
以及其他一些变化.我需要类似第二个选项的内容,该选项还可以保留该索引之后的子级.我想念什么?我可以拼接一下,然后以某种方式重新分配孩子吗?
and some other variations on that. I need something like the second option that preserves the children after that index as well. What am I missing? Can I splice it and reassign the children somehow?
推荐答案
您可以使用eq()
通过其索引将集合中的元素作为目标.试试这个:
You can use eq()
to target an element in a collection by its index. Try this:
this.element.find('.nav-tabs').children().eq(index).remove();
splice()
和slice()
对您不起作用的原因是因为它们返回DOMElements数组,而不是jQuery对象.
The reason splice()
and slice()
don't work for you is because they return an array of DOMElements, not a jQuery object.
这篇关于删除索引处的单个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!