2017-02-13 6 views
1

HTMLすべての要素を削除する方法jQueryの最初の要素と2番目の要素を除きますか?上記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> 

私はjqueryの最初の2要素以外のすべての要素を削除する方法... jqueryの内<div class='geo_select'>内のすべての<h3>除く要素とデフォルトのコンテンツ<div>を削除したいですか?上記のシナリオでは?

+0

すべての動的コンテンツに追加のクラスを与え、そのクラスのすべてのdiv要素を削除します。それは簡単でしょうか? – Visrozar

答えて

2

必要に応じてCSSを使用できます。

.geo_select > div:not(:first-of-type) { 
    display:none; 
} 
4


やCSSと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() 
は、単にそれを隠すことを行うには、いくつかの方法があります。

.geo_select > div:nth-of-type(n+3){ 
    display:none; 
} 
+1

ありがとう@Pranav C Balan –

関連する問題