简体   繁体   English

如何使用jQuery完全删除div onclick?

[英]How to completely remove div onclick with jquery?

There are some similar questions however my specific problem is an error in my code. 有一些类似的问题,但是我的特定问题是代码中的错误。 I am able to remove the jumbotron div but there is a portion of it that still shows and I'm unsure why. 我可以删除jumbotron div,但是其中一部分仍然显示,我不确定为什么。

Here is the jsfiddle https://jsfiddle.net/dominiconorton/2bdxvrwq/2/ 这是jsfiddle https://jsfiddle.net/dominiconorton/2bdxvrwq/2/

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Blank</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Blank <span class="sr-only">(current)</span></a>
            </li>
    </ul>
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Sign out</button>
  </div>
</nav>

<!-- Search tutorial -->
<div class="jumbotron" id="searchTutorial">
    <div class="container">
        <h1 class="display-4">Lorem Ipsum</h1>
        <p> Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit 
        </p>

    <div class="removeDiv">
    <p>
            <a class="btn btn-primary btn-lg " href="#" role="button">Close tutorial</a>
    </p>
    </div>

    </div>
</div>
// close jumbotron //
jQuery(function($) {
  $('.removeDiv').on('click', function() {
    $(this).parent('div').remove();
  });
});

I'm using Bootstrap 4.0 and JQuery 3.3.1 我正在使用Bootstrap 4.0和JQuery 3.3.1

I tried changing the parent of the div so that the #searchTutorial is the parent and this didn't work as expected 我尝试更改div的父级,以使#searchTutorial为父级,这无法按预期工作

I would like to completely remove the jumbotron div onclick of the close tutorial 我想完全删除关闭教程的jumbotron div onclick

Your javascript is removing the direct parent of the div being clicked, which is actually div.container . 您的JavaScript删除了所单击的div的直接父级,它实际上是div.container This isn't obvious because the indenting isn't consistent. 这不是很明显,因为缩进不一致。

I would actually do @mark.hch's idea and use $(this).closest('.jumbotron').remove() . 我实际上会执行@ mark.hch的想法,并使用$(this).closest('.jumbotron').remove()

If you can change the structure, you might consider using <section> instead of <div> for the major elements, so that you can use $(this).closest('section').remove(); 如果可以更改结构,则可以考虑使用<section>而不是<div>作为主要元素,以便可以使用$(this).closest('section').remove();

您必须指定班级

jQuery(function($) {$('.removeDiv a').on('click', function() {$('.jumbotron').remove();});

removeDiv的父级是容器div,单击remove div即可按预期工作,它会删除其父级,而不是整个jumbotron div,这就是为什么在删除容器div后会出现灰色区域的原因,因此,您必须直接选择jumbotron div或选择父级的父级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM