HTML
<div class="geo_select">
<h3>header 3</h3>
<div class="row form-group">
default content
</div>
<div class="row form-group">
//Dynamic content 1 here
</div>
<div class="row form-group">
//Dynamic content 2 here
</div>
</div>
在上面的 HTML 代码中,我想删除除 <h3>
之外的所有元素和默认内容 <div>
在<div class='geo_select'>
里面在 jquery 中.. 如何删除 jquery 中除前 2 个元素之外的所有元素?在我上面的场景中?
最佳答案
在 jQuery 中有几种方法可以做到这一点
// use this, if there are different types of elements as child
$('.geo_select > div:nth-of-type(n+3)').remove()
// use any of these if childs are same
$('.geo_select > div:nth-child(n+3)').remove()
$('.geo_select > div:gt(2)').remove()
// this is jQuery way which reduce the set into the range
$('.geo_select > div').slice(2).remove()
或者使用 css,简单地隐藏它。
.geo_select > div:nth-of-type(n+3){
display:none;
}
关于javascript - 如何删除jQuery中除第一个元素和第二个元素之外的所有元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42200959/