简体   繁体   English

使用JQuery根据子元素删除div

[英]Remove div based on child element using JQuery

I have a list of divs similar to the following: 我有一个类似于以下内容的div列表:

<div class="row"><a data-card-uid="not me"></a></div>
<div class="row"><a data-card-uid="not me"></a></div>
<div class="row"><a data-card-uid="THIS ONE"></a></div>
<div class="row"><a data-card-uid="not me"></a></div>
<div class="row"><a data-card-uid="not me"></a></div>
<div class="row"><a data-card-uid="not me"></a></div>

I'd like to remove the div which has 我想删除具有

data-card-uid="THIS ONE"

How can I do this? 我怎样才能做到这一点? I understand you can use .remove() to remove the corresponding div but I'm not sure how to select the appropriate one. 我知道您可以使用.remove()删除相应的div,但是我不确定如何选择合适的div。

首先使用Attribute Equals Selector选择要删除其父元素的元素,然后在parent()上调用remove() parent()

$('[data-card-uid="THIS ONE"]').parent().remove();

如果要删除内容为data-card-uid =“ THIS ONE”的div,请使用:

$('[data-card-uid="THIS ONE"]').parent().remove();

You need to remove the parent. 您需要删除父项。 So 所以

$('[data-card-uid="THIS ONE"]').parent().remove();

See it here JSFiddle 见这里的jsfiddle

 $(document).ready(function() { $('[data-card-uid="THIS ONE"]').parent(".row").remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="row"> <a data-card-uid="not me">not me</a> </div> <div class="row"> <a data-card-uid="not me">not me</a> </div> <div class="row"> <a data-card-uid="THIS ONE">this one</a> </div> <div class="row"> <a data-card-uid="not me">not me</a> </div> <div class="row"> <a data-card-uid="not me"></a> </div> <div class="row"> <a data-card-uid="not me">not me</a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM