Bal*_*r B 1 html javascript css jquery
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>
Run Code Online (Sandbox Code Playgroud)
在上面的HTML代码我想删除除jquery 里面的所有元素<h3>和默认内容..如何删除除jquery中的前2个元素之外的所有元素?在我上面的场景?<div><div class='geo_select'>
在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()
Run Code Online (Sandbox Code Playgroud)
或者用css,只需隐藏它.
.geo_select > div:nth-of-type(n+3){
display:none;
}
Run Code Online (Sandbox Code Playgroud)