我希望能够通过单击按钮删除div,div是引导程序中的卡 这是div:
$("button").click(function () {
$(this).parent().closest('div').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class ="col-md-4 col-xs-4">
<div class="card" >
<img class="card-img-top" src="img/cheesecake-cream.jpg" alt="Chocolate cream" />
<div class="card-body">
<h4 class="card-title">Chocolate cream</h4>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
<div class= "card-body clearfix" >
<div class="text-left text-success float-left">
<i class="fa fa-pencil" aria-hidden="true"></i>
<span id='clickableAwesomeFont'> Edit</span>
</div>
<div class="text-right text-danger float-right">
<i class="fa fa-trash" aria-hidden="true"></i>
<span id='clickableAwesomeFont'> Delete</span>
<button>Remove</button>
</div>
</div>
</div>
</div>
我想在单击按钮时删除此div。
提前致谢。
答案 0 :(得分:2)
有一个jquery .col-md-4
,它将通过传递它将找到该元素的任何选择器来找到父元素。因此,在您的示例中,您必须通过该选择器传递类名.card
或$("button").click(function () {
$(this).parents(".col-md-4").hide();
});
,您可以执行任何操作
.col-md-4{border:1px red solid
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class ="col-md-4 col-xs-4">
<div class="card" >
<img class="card-img-top" src="img/cheesecake-cream.jpg" alt="Chocolate cream">
<div class="card-body">
<h4 class="card-title">Chocolate cream</h4>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
<div class= "card-body clearfix" >
<div class="text-left text-success float-left">
<i class="fa fa-pencil" aria-hidden="true"></i><span id='clickableAwesomeFont'> Edit</span>
</div>
<div class="text-right text-danger float-right">
<i class="fa fa-trash" aria-hidden="true"></i> <span id='clickableAwesomeFont'> Delete</span>
<button>Remove</button>
</div>
</div>
</div>
</div>
<div class ="col-md-4 col-xs-4">
<div class="card" >
<img class="card-img-top" src="img/cheesecake-cream.jpg" alt="Chocolate cream">
<div class="card-body">
<h4 class="card-title">Chocolate cream</h4>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
<div class= "card-body clearfix" >
<div class="text-left text-success float-left">
<i class="fa fa-pencil" aria-hidden="true"></i><span id='clickableAwesomeFont'> Edit</span>
</div>
<div class="text-right text-danger float-right">
<i class="fa fa-trash" aria-hidden="true"></i> <span id='clickableAwesomeFont'> Delete</span>
<button>Remove</button>
</div>
</div>
</div>
</div>
<div class ="col-md-4 col-xs-4">
<div class="card" >
<img class="card-img-top" src="img/cheesecake-cream.jpg" alt="Chocolate cream">
<div class="card-body">
<h4 class="card-title">Chocolate cream</h4>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
<div class= "card-body clearfix" >
<div class="text-left text-success float-left">
<i class="fa fa-pencil" aria-hidden="true"></i><span id='clickableAwesomeFont'> Edit</span>
</div>
<div class="text-right text-danger float-right">
<i class="fa fa-trash" aria-hidden="true"></i> <span id='clickableAwesomeFont'> Delete</span>
<button>Remove</button>
</div>
</div>
</div>
</div>
parent
答案 1 :(得分:1)
使用jQuery#parents方法并提供要隐藏的选择器。
如果您想要remove
内容而不是hide
,请使用
删除
$(this).parents('.col-md-4.col-xs-4').parent().empty();
隐藏
$(this).parents('.col-md-4.col-xs-4').hide();
<强>代码强>
$("button").click(function () {
$(this).parents('.col-md-4.col-xs-4').hide();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class ="col-md-4 col-xs-4">
<div class="card" >
<img class="card-img-top" src="img/cheesecake-cream.jpg" alt="Chocolate cream">
<div class="card-body">
<h4 class="card-title">Chocolate cream</h4>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
<div class= "card-body clearfix">
<div class="text-left text-success float-left">
<i class="fa fa-pencil" aria-hidden="true"></i><span id='clickableAwesomeFont'> Edit</span>
</div>
<div class="text-right text-danger float-right">
<i class="fa fa-trash" aria-hidden="true"></i> <span id='clickableAwesomeFont'> Delete</span>
<button>Remove</button>
</div>
</div>
</div>
</div>
&#13;
答案 2 :(得分:1)
您可以使用解决方案
$("button").click(function () {
$(this).closest('div[class="col-md-4 col-xs-4"]').slideUp("slow", function(){
$(this).remove();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-md-4 col-xs-4">
<div class="card" >
<img class="card-img-top" src="img/cheesecake-cream.jpg" alt="Chocolate cream" />
<div class="card-body">
<h4 class="card-title">Chocolate cream</h4>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
<div class= "card-body clearfix" >
<div class="text-left text-success float-left">
<i class="fa fa-pencil" aria-hidden="true"></i>
<span id='clickableAwesomeFont'> Edit</span>
</div>
<div class="text-right text-danger float-right">
<i class="fa fa-trash" aria-hidden="true"></i>
<span id='clickableAwesomeFont'> Delete</span>
<button>Remove</button>
</div>
</div>
</div>
</div>
我使用jQuery closest
方法获取div而slideUp
代替hide
来获取动画。
slideUp
完成后,它将删除div(这是一个回调)。
希望这会对你有所帮助。
答案 3 :(得分:0)
closest
只升级一级。您应该使用$("button").click(function () {
$(this).closest('div.col-md-4.col-xs-4').remove();
});
:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/slidingLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/red"
app:layout_scrollFlags="scroll|enterAlways"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="20dp"
android:background="@drawable/rounded_background"
android:orientation="horizontal"
android:padding="6dp"
app:layout_scrollFlags="scroll|enterAlways">
<EditText
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="6"
android:background="@null"
android:fontFamily="sans-serif-light"
android:hint="Unesite grad"
android:paddingLeft="16dp"
android:paddingStart="16dp"/>
<ImageView
android:id="@+id/cancelSearch"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:padding="10dp"
android:src="@drawable/ic_cancel" />
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/appbar"
android:background="#ffffff"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
答案 4 :(得分:0)
$("button").click(function () {
$(this).parent('div.card').hide();
});