[英]How to removed repeated element with same class in parent div using jquery
I want to remove repeated element of same class in parent div. 我想删除父div中相同类的重复元素。 I have used following code but it's remove all the element except first. 我使用了以下代码,但它删除了除第一个元素以外的所有元素。
var found = {};
$('.product-bottom').each(function(){
var $this = $(this);
if(found[$this.data('id')]){
$this.remove();
}
else{
found[$this.data('id')] = true;
}
});
Following is my HTML.. 以下是我的HTML ..
<div class="product-bottom">
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
</div>
<div class="product-bottom">
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
</div>
<div class="product-bottom">
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
</div>
I want to remove repeated custom_product
class element in each parent div product-bottom
. 我想删除每个父div product-bottom
中重复的custom_product
类元素。 So my output will be.. 所以我的输出将是..
<div class="product-bottom">
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
</div>
<div class="product-bottom">
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
</div>
<div class="product-bottom">
<div class="col-lg-12"><span>Title</span></div>
<a href="#" class="button custom_product" data-quantity="1"></a>
</div>
Check this code here just I emoved the second element with selector I hope its help you 在这里检查此代码,只是我用选择器移除了第二个元素,希望对您有所帮助
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <script> $(document).ready(function() { $('.product-bottom .col-lg-12:nth-of-type(2)').remove(); $('.product-bottom a:nth-of-type(2)').remove(); }); </script> </head> <div class="product-bottom"> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1"></a> <div class="col-lg-12"><span>Title 1</span></div> <a href="#" class="button custom_product" data-quantity="1"></a> </div> <div class="product-bottom"> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1"></a> <div class="col-lg-12"><span>Title 1</span></div> <a href="#" class="button custom_product" data-quantity="1"></a> </div> <div class="product-bottom"> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1"></a> <div class="col-lg-12"><span>Title 1</span></div> <a href="#" class="button custom_product" data-quantity="1"></a> </div> <body> </body> <html>
You can try with :gt()
selector that selects all elements at an index greater than the index within the matched set. 您可以尝试使用:gt()
选择器,该选择器选择索引大于匹配集中索引的所有元素。
var el = $(this).find('.button.custom_product:gt(0)');
Demo: 演示:
$('.product-bottom').each(function(){ var el = $(this).find('.button.custom_product:gt(0)'); el.prev('div').remove(); el.remove(); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="product-bottom"> <div class="col-lg-12"><span>Title1 of 1</span></div> <a href="#" class="button custom_product" data-quantity="1">1 of 1</a> <div class="col-lg-12"><span>Title2 of 1</span></div> <a href="#" class="button custom_product" data-quantity="1">2 of 1</a> </div> <div class="product-bottom"> <div class="col-lg-12"><span>Title1 of 2</span></div> <a href="#" class="button custom_product" data-quantity="1">1 of 2</a> <div class="col-lg-12"><span>Title2 of 2</span></div> <a href="#" class="button custom_product" data-quantity="1">2 of 2</a> </div> <div class="product-bottom"> <div class="col-lg-12"><span>Title1 of 3</span></div> <a href="#" class="button custom_product" data-quantity="1">1 of 3</a> <div class="col-lg-12"><span>Title2 of 3</span></div> <a href="#" class="button custom_product" data-quantity="1">2 of 3</a> </div>
If the structure remains the same, you can use the :last-of-type
pseudo-class to remove the repeated content. 如果结构保持不变,则可以使用:last-of-type
伪类删除重复的内容。
$(document).ready(function() { $('.product-bottom div:last-of-type').remove(); $('.product-bottom .custom_product:last-of-type').remove(); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="product-bottom"> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1">1 of 1</a> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1">2 of 1</a> </div> <div class="product-bottom"> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1">1 of 2</a> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1">2 of 2</a> </div> <div class="product-bottom"> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1">1 of 3</a> <div class="col-lg-12"><span>Title</span></div> <a href="#" class="button custom_product" data-quantity="1">2 of 3</a> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.