最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I delete this bootstrap div - Stack Overflow

programmeradmin4浏览0评论

I want to be able to remove a div at the click of a button, the div is a card in bootstrap this is the div:

$("button").click(function () {
  $(this).parent().closest('div').hide();
});
<script src=".1.1/jquery.min.js"></script>
<link href=".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'>&nbsp;Edit</span>
      </div>
      <div class="text-right text-danger float-right">
        <i class="fa fa-trash" aria-hidden="true"></i>
        <span id='clickableAwesomeFont'>&nbsp;Delete</span>
        <button>Remove</button>                  
      </div>
    </div>
  </div>
</div>

I want to be able to remove a div at the click of a button, the div is a card in bootstrap this is the div:

$("button").click(function () {
  $(this).parent().closest('div').hide();
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn./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'>&nbsp;Edit</span>
      </div>
      <div class="text-right text-danger float-right">
        <i class="fa fa-trash" aria-hidden="true"></i>
        <span id='clickableAwesomeFont'>&nbsp;Delete</span>
        <button>Remove</button>                  
      </div>
    </div>
  </div>
</div>

I want to delete this div when I click the button.

Thanks in advance.

Share Improve this question edited Sep 22, 2017 at 5:57 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Sep 22, 2017 at 5:36 letmebeletmebe 1353 silver badges9 bronze badges 3
  • do you want to delete <div class ="col-md-4 col-xs-4"> this div? – Pankaj Makwana Commented Sep 22, 2017 at 5:38
  • I believe that will remove the button also – rafon Commented Sep 22, 2017 at 5:39
  • @PankajMakwana yes i want to delete that div – letmebe Commented Sep 22, 2017 at 5:47
Add a ment  | 

5 Answers 5

Reset to default 3

There is a jquery parents() which will find the parent element by passing any selector it will find that element. So here in your example you have to pass class name .col-md-4 OR .card by that selector you can perform any action

$("button").click(function () {
    $(this).parents(".col-md-4").hide();
});
.col-md-4{border:1px red solid
}
<script src="https://ajax.googleapis./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'>&nbsp;Edit</span>
                  </div>
                  <div class="text-right text-danger float-right">
                    <i class="fa fa-trash" aria-hidden="true"></i> <span id='clickableAwesomeFont'>&nbsp;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'>&nbsp;Edit</span>
                  </div>
                  <div class="text-right text-danger float-right">
                    <i class="fa fa-trash" aria-hidden="true"></i> <span id='clickableAwesomeFont'>&nbsp;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'>&nbsp;Edit</span>
                  </div>
                  <div class="text-right text-danger float-right">
                    <i class="fa fa-trash" aria-hidden="true"></i> <span id='clickableAwesomeFont'>&nbsp;Delete</span>
                    <button>Remove</button>                  
                  </div>
                </div>
            </div>

        </div>

Use jQuery#parents method and give the selector which you want to hide. If you want to remove the content instead of hide, use

For remove

$(this).parents('.col-md-4.col-xs-4').parent().empty();

For hide

$(this).parents('.col-md-4.col-xs-4').hide();

Code

$("button").click(function () {
    $(this).parents('.col-md-4.col-xs-4').hide();
});
<script src="https://ajax.googleapis./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'>&nbsp;Edit</span>
             </div>
             <div class="text-right text-danger float-right">
                <i class="fa fa-trash" aria-hidden="true"></i> <span id='clickableAwesomeFont'>&nbsp;Delete</span>
                <button>Remove</button>                  
             </div>
         </div>
    </div>

</div>

Here you go with a solution

$("button").click(function () {
  $(this).closest('div[class="col-md-4 col-xs-4"]').slideUp("slow", function(){
    $(this).remove();
  });
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn./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'>&nbsp;Edit</span>
      </div>
      <div class="text-right text-danger float-right">
        <i class="fa fa-trash" aria-hidden="true"></i>
        <span id='clickableAwesomeFont'>&nbsp;Delete</span>
        <button>Remove</button>                  
      </div>
    </div>
  </div>
</div>

I've used jQuery closest method to get the div and slideUp instead of hide to get the animation.

After slideUp pletes it will remove the div (it's a callback).

Hope this will help you.

parent only goes one level up. You should use closest:

$("button").click(function () {
    $(this).closest('div.col-md-4.col-xs-4').remove();
});
$("button").click(function () {
    $(this).parent('div.card').hide();
});
发布评论

评论列表(0)

  1. 暂无评论