使用jQuery单击“ X”时,如何删除包含图像的div?

Ker*_*ham 6 jquery

我制作了一个带有图像的盒子,在右上角有一个X。当我单击它时,我希望它删除图像,框和X。但是什么也没发生。请不要苛刻,我是Jquery和javascript的新手。

<span class="removethis">    
<div class="imformationbox1" >  
<div class="col-lg-12">
<button type="button" class="deletebutton" id="" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;<  /span></button>    
<a href="#" id="" title="Image 2"><img src="http://placehold.it/250x250 class="thumbnail img-responsive"></a>    
</div>
</div> 
</span>

<script>
$( ".deletebutton" ).click(function() {
$( ".removethis" ).remove();
});
</script>
Run Code Online (Sandbox Code Playgroud)

Moh*_*sef 3

它非常简单..只需使用closest()

<script>
$(document).on('click',".deletebutton" ,function() {
 $(this).closest(".removethis" ).remove();
});
</script>
Run Code Online (Sandbox Code Playgroud)

或者你可以使用parent()

<script>
    $(document).on('click',".deletebutton" ,function() {
     $(this).parent().parent().parent().remove();
    });
</script>
Run Code Online (Sandbox Code Playgroud)

如果您动态附加此数据,只需使用

$(document).on('click',".deletebutton" , function(){});  
Run Code Online (Sandbox Code Playgroud)