【问题标题】:How to make a div disappear on click?如何让 div 在点击时消失?
【发布时间】:2021-01-25 13:08:53
【问题描述】:

我整天都在努力寻找一个简单的纯css / html代码,只是为了在单击按钮时使div消失。我更喜欢 CSS,但如果不可能,我会改用 JS 解决方案。

很多关于溢出的解决方案,但大多数使用 js。请查看下面的代码并在下面添加示例或建议。感谢您的帮助,Stewy

body {
    background-color: #6B6B6B;
    font-family: Arial;
    color: grey;
    font-size: 12px;
    margin: 0px;
}
    /*............... bgcover ...............*/

    .bgcover {
    position: fixed;
    top: 50px;
    left: 50px;
    width: 200px;
    height: 200px;
    background-color: rgba(0, 0, 0, .4);
}
    .call {
    position: fixed;
    top: 100px;
    left: 100px;
}
    /*........ crossfade on buttons ........*/

    .hover,.nohover{
    transition:.3s;
    position:absolute;
}
    .nohover{
    opacity:0;
}
    a:hover .hover{
    opacity:0;
}
    a:hover .nohover{
    opacity:1;
}
<div class="bgcover" align="center">
<p>Button turns off this bg cover div.</p>
</div>

<div class="call">
<a href="bgcoveroff.htm">
<img src="http://wizzfree.com/pix/call2.png" width="100" class="nohover">
<img src="http://wizzfree.com/pix/call.png" width="100" class="hover"></a>
</div>

【问题讨论】:

    标签: html css button onclick divide


    【解决方案1】:

    你必须在标签中传递内联 JS。不仅可以隐藏元素。 onclick 属性应该需要触发。 喜欢这个&lt;a href="#" id="btn" onclick="this.focus();return false"&gt;Click&lt;/a&gt; 嗨 Stewy,请检查此代码。就是内联js的例子。

        body {
        background-color: #6B6B6B;
        font-family: Arial;
        color: grey;
        font-size: 12px;
        margin: 0px;
    }
        /*............... bgcover ...............*/
    
        .bgcover {
        position: fixed;
        top: 50px;
        left: 50px;
        width: 200px;
        height: 200px;
        background-color: rgba(0, 0, 0, .4);
    }
        .call {
        position: fixed;
        top: 100px;
        left: 100px;
    }
        /*........ crossfade on buttons ........*/
    
        .hover,.nohover{
        transition:.3s;
        position:absolute;
    }
        .nohover{
        opacity:0;
    }
        a:hover .hover{
        opacity:0;
    }
        a:hover .nohover{
        opacity:1;
    }
    
    .hidden{
        display:none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="bgcover" id=box align="center">
    <p>Button turns off this bg cover div.</p>
    </div>
    
    <div class="call">
      <a id="btn" onclick="document.getElementById('box').classList.toggle('hidden');" >
    <img src="http://wizzfree.com/pix/call2.png" width="100" class="nohover">
    <img src="http://wizzfree.com/pix/call.png" width="100" class="hover"></a>
    </div>

    【讨论】:

    • Thx Vishwa,我无法让您的解决方案正常工作?你能添加到我的代码中吗?谢谢...
    • 好的,我正在尝试适应你的代码,你会明白的。
    • thx 最后一个问题,你能让按钮也消失吗?
    • 你的按钮图片坏了。为什么不用 CSS 做按钮?
    【解决方案2】:
    1. 定义交互式节点和目标节点。
    2. 创建事件监听器来处理点击事件。
    3. 创建一个隐藏第二个元素的函数
      const button = document.getElementById('button'); // or classname, whatever. it is your link or any node element instead of it. 
      const div = document.getElementById('targetDiv');
    
      function toggleDiv(e) {
        // e - event object - {button}.
        e.preventDefault(); // only if you use <a> as node.
        
        div.classList.add("my-classname-to-hide-div");
      }
    
      button.addEventListener('click', toggleDiv, false);
    

    css

      .my-classname-to-hide-div {
        display: none; 
        // or 
        opacity: 0;
        visibility: hidden;
        transition: all 0.2s all;
      }
    

    ps。如果你使用 JQuery,代码会更简单,你可以使用内置的过渡效果。

    【讨论】:

    • thx goris,但是您可以将您的解决方案添加到我的代码中吗?所以它工作......
    • 你有单独的 js 文件还是只有 html/css?你用的是es6还是老版本的js?你有 jquery 或任何其他 js 库吗?
    • jsfiddle.net/Wentris71/o4yc2agt/8 - 这是一个可执行代码。
    • 很好的例子 goris,是否可以让按钮也消失?
    • 当然,将链接 divtarget 重置为您可能需要的任何内容。随意尝试))
    猜你喜欢
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多