2016-08-11 6 views
1

私のウェブサイトはdiv要素を複製しています。複製の理由を調べる前に、複製された複製を一時的に隠す方法を検討しています。divJQueryで重複したdivクラスを削除します

$(document).ready(function() { 
    $('.ms-account-wrapper').eq(1).remove(); 
}); 

HTML::

<div class="ms-account-wrapper">1</div> 
<div class="ms-account-wrapper">2</div> <---want to remove this one 

任意のアイデア

は、私は次のスクリプトを試してみましたか?

感謝

最大

+0

謝罪は... .js '> – memaxt

+0

あなたの謝罪 –

答えて

0

はjQueryのセレクタは、そのクラスを持つすべての要素の配列を返します。

$(document).ready(function() { 
    var _divs = $('.ms-account-wrapper'); 
    // Ensure there is more than 1 
    if (_divs.length > 1) 
    _divs[1].remove(); 
}); 

ここcodepenです:あなたは、配列のインデックスはなりあなたのような第二の要素を参照することができます。

3
選択

remove()全ての第1を除く.ms-account-wrapperクラスを持つ要素:

$(".ms-account-wrapper:not(:first)").remove(); 

これはどのように多くの.ms-account-wrapper要素に関係なく動作します

Working example

1

あり、これを試してみてください。

<script> 
$(document).ready(function() { 
$('.ms-account-wrapper:last').remove(); 
}); 
</script> 
0

あなたがこの

$(document).ready(function() { 
$('.ms-account-wrapper').not(':first').remove(); 
}); 

実施例(ここではデモの目的のために3秒後に削除されます)

setTimeout(function(){$('.ms-account-wrapper').not(':first').remove();},3000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="ms-account-wrapper">1</div> 
 
<div class="ms-account-wrapper">2</div> 
 
<div class="ms-account-wrapper">3</div> 
 
<div class="ms-account-wrapper">4</div> 
 
<div class="ms-account-wrapper">5</div>

-2
$(document).ready(function() { 
    var _divs = $('.ms-account-wrapper'); 
    // Ensure there is more than 1 
    if (_divs.length > 1){ 
     for(var i=1;i<_divs.length;i++) 
      _divs[i].remove(); 
    } 
}); 

あなたはこれを試すことができます...それを達成することができます2以上の場合でも動作します

0

これを試してください: <スクリプトタイプ= 'テキスト/ javascriptの' SRC = 'のhttp://code.jquery.com/jquery-1.6.3私は拡張子を含めforgottonを持っていた

<script> 
    $(document).ready(function() { 
     $('.ms-account-wrapper').first().nextAll().remove(); 
    }); 
</script> 
上記のコードは動作します
関連する問題