2016-08-11 114 views
1

由於某些奇怪的原因,我的網站正在複製div元素。在我試圖找到它被重複的原因之前,我正在尋找一種方法來暫時隱藏重複的div使用JQuery刪除重複的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

道歉上面的代碼工作...我已經福爾戈包括進一步擴展: <腳本類型= '文/ JavaScript的' SRC =「HTTP://code.jquery.com/jquery-1.6.3 .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().ms-account-wrapper類的所有元素,除了第一:有

Working example

1

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

這個工程有多少.ms-account-wrapper元素,無論試試這個:

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

你可以做到這一點

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

工作實例

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(); 
    } 
}); 

(此處爲3秒演示目的後會刪除),你可以試試這個...它作品如果超過2也

0

試試這個: -

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