2016-08-11 110 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>