我希望能够通过单击按钮删除一个 div,该 div 是 bootstrap 中的一张卡片 这是 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。
提前致谢。
最佳答案
有一个 jquery parents()
它将通过传递任何选择器找到父元素,它将找到该元素。因此,在您的示例中,您必须通过该选择器传递类名 .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>
关于javascript - 我怎样才能删除这个 bootstrap div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46357523/