用jQuery删除3个最后的div [英] remove 3 last divs with jQuery
本文介绍了用jQuery删除3个最后的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<div id="widgetAreaFooter">
<div class="row">1</div>
<div class="row">2</div>
<div class="row">3</div>
<div class="row">4</div>
<div class="row">5</div>
<div class="row">6</div>
<div class="row">7</div>
</div>
如何删除最后3个div?
How to remove the 3 last div ?
我尝试了这个,但是不起作用:/
I tryed this but it doesn't work :/
var row = $( '#widgetAreaFooter>.row' );
var nbr = row.length ;
for ( var i=4;i<nbr;i++ ) row.get(i).remove();
or
for ( var i=4;i<nbr;i++ ) row[i].remove();
推荐答案
这将删除最后三个元素:
This will remove the last three elements:
$('#widgetAreaFooter > .row').slice(-3).remove();
-
您可以使用
.slice()
来获取jQuery集合的一部分. >
You can get a part of a jQuery collection using
.slice()
.
如果提供负数, 这表明一个职位开始 从集合的末尾开始,而不是 开始.
If a negative number is provided, this indicates a position starting from the end of the set, rather than the beginning.
这篇关于用jQuery删除3个最后的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文