隐藏元素父jquery

时间:2013-09-11 13:14:29

标签: jquery parent-child parent

大家好我有这种元素结构当我点击删除包含类shared_box的框应该隐藏它。我尝试用parent()和最近但不起作用。这是我的计划。

              <div class="shared_box"> <!--Element to hide -->
                    <div class="user_post">
                     <img src="#" class="pull-left">

                     <!-- Single button -->
                        <div class="btn-group pull-right action_post" style="display: block;">
                          <button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                            <span class="caret"></span>
                          </button>
                          <ul class="dropdown-menu" role="menu">
                            <!-- element clicked -->
                            <li><a href="#" class="delete" id="13">Delete</a></li>
                            <li><a href="#">Hide Post</a></li>

                          </ul>
                        </div>  
                       </div>
                  </div>

修改

JS

$(document).on("click", ".delete", function() {
            var id_post = $(this);
            bootbox.confirm("Are you sure do want delete?", function(result) {
               if (result) {

                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url:  CI_ROOT + 'delete_post',
                        data: "id_post=" + id_post.attr('id') ,
                        success: function(val) {
                            id_post.closest('div.shared_box').hide();
                                    // this one doesn't work
                        }

                    }); 

                } else {
                    console.log("close");
                }
            });
});

1 个答案:

答案 0 :(得分:0)

你必须使用parents()而不是parent(),因为parent()只检查直接祖先,而parents()检查DOM中的所有三个。

这样的事情:

$('.delete').on('click',function(){
    $(this).parents('.shared_box').hide();
});