Javascript 删除单击的元素所在的Div

Javascript 删除单击的元素所在的Div,javascript,jquery,html,Javascript,Jquery,Html,当您单击div内的href时,我正试图从页面中删除父div。同一个div可以有多个,或者我只使用类名。我试图使用$(this.remove();但这只会删除删除按钮。我已经研究了父属性,但我不确定我是否完全理解它是如何工作的 这就是我所拥有的: <script> $(".delete-button").click(function(){ $(this).remove(); }); </script> <div

当您单击div内的href时,我正试图从页面中删除父div。同一个div可以有多个,或者我只使用类名。我试图使用$(this.remove();但这只会删除删除按钮。我已经研究了父属性,但我不确定我是否完全理解它是如何工作的

这就是我所拥有的:

<script>        
 $(".delete-button").click(function(){
    $(this).remove();           
 });
</script>


 <div class="bottomsurgicalhistory">

  "Div Content is Here"
  <li class="delete"><a href="#"  class="delete-button"></a></li>
 </div>

 <div class="bottomsurgicalhistory">

  "Div Content is Here"
  <li class="delete"><a href="#"  class="delete-button"></a></li>
 </div>

$(“.delete按钮”)。单击(函数(){
$(this.remove();
});
“Div内容在这里”
  • “Div内容在这里”

  • 感谢您的帮助

    如果脚本标记位于元素之前,则需要一个dom就绪函数,并且在使用哈希作为href时,防止默认滚动到顶部通常是个好主意。单击删除按钮时,使用
    closest()
    查找与
    bottomsurgerogichistory
    类匹配的最近父级:

    $(function() {
        $(".delete-button").on('click', function(e){
            e.preventDefault();
            $(this).closest('.bottomsurgicalhistory').remove();           
         });
    });
    

    如果脚本标记位于元素之前,则需要一个dom就绪函数,并且在使用哈希作为href时,防止默认滚动到顶部通常是个好主意。单击删除按钮时,使用
    closest()
    查找与
    bottomsurgerogichistory
    类匹配的最近父级:

    $(function() {
        $(".delete-button").on('click', function(e){
            e.preventDefault();
            $(this).closest('.bottomsurgicalhistory').remove();           
         });
    });
    

    只需替换以下代码:

    $(this).remove();
    
    对于这一个:

    $(this).parent().remove();
    

    只需替换以下代码:

    $(this).remove();
    
    对于这一个:

    $(this).parent().remove();
    
    试试这个

    $(this).parent().remove();
    
    试试这个

    $(this).parent().remove();
    

    请尝试
    $(this).closest(“.bottomsurgerogichistory”).remove()。在HTML中,您可以轻松地使用
    $(this).parent().remove()。以下是
    closest()
    的文档:。当您不确定元素的嵌套方式时,使用它是很好/可靠的,但是您知道某些祖先将匹配选择器,并且会注意到LI元素应该在UL中,我猜这只是伪代码,但仍然!请尝试
    $(this).closest(“.bottomsurgerogichistory”).remove()。在HTML中,您可以轻松地使用
    $(this).parent().remove()。以下是
    closest()
    的文档:。当您不确定元素的嵌套方式时,使用它是很好/可靠的,但是您知道某些祖先将匹配选择器,并且会注意到LI元素应该在UL中,我猜这只是伪代码,但仍然!