简体   繁体   English

如何仅在单击时显示一个元素并在单击其他元素时隐藏它?

[英]How to show an element only on click on it and and hide it on click on other elements?

Description: I created two buttons and two more elements (div & section). 说明:我创建了两个按钮和另外两个元素(div和section)。 When I click on button 1, div element will appear with background-color HotPink and/if at this moment i re-click on button 1, div element will disappear. 当我单击按钮1时,div元素将显示为背景色HotPink,并且//如果此时重新单击按钮1,则div元素将消失。

I also wrote a function for button 2 so that when i click on button 2, section element will appear with background-colour DarkGreen and at this moment when i click on button 2 again, section element will disappear. 我还为按钮2编写了一个函数,以便当我单击按钮2时,该部分元素将以背景色DarkGreen出现,并且此刻,当我再次单击按钮2时,该部分元素将消失。

I should mention that if i click on white space of body ( (document).click(event) ), both div and section elements will disapear. 我应该提到,如果我单击正文的空白((document).click(event)),则div和section元素都将消失。

Question: What function should I write to show div element when I click on button 1 and then I hide hide it when I click on button 2 or any other elements on my web page??? 问题:单击按钮1时应该编写什么函数来显示div元素,然后单击按钮2或网页上的任何其他元素时将其隐藏起来?

Demo 演示

NOTE: I duplicated this question because I'm not interested to use any method like: 注意:我重复了此问题,因为我不喜欢使用任何方法,例如:

$(".button1").click(function(event){
    event.stopPropagation();
    if($(".div").css("display") == "none"){
    $(".div").css("display","block");
    $(".section").css("display","none");
    }else{
    $(".div").css("display","none");
    }
    });
    $(".button2").click(function(event){
    event.stopPropagation();
    if($(".section").css("display") == "none"){
    $(".section").css("display","block");
$(".div").css("display","none");
    }else{
    $(".section").css("display","none");
    }
    });


It would be better if you do a favour and suggest a better method instead of duplicating a line of code with different class name under different events (functions). 如果您愿意帮忙并提出一个更好的方法,而不是在不同的事件(函数)下复制一行具有不同类名的代码,那会更好。

My codes: HTML: 我的代码: HTML:

<button class="button1">
Show Div Element
</button>
<div class="div">
I am Div Element
</div>

<br><br>

<button class="button2">
Show Section Element
</button>
<section class="section">
I am Section Element
</section>

JQuery: JQuery的:

$(function(){

    $(".button1").click(function(event){
    event.stopPropagation();
    if($(".div").css("display") == "none"){
        $(".div").css("display","block");
    }else{
        $(".div").css("display","none");
    }
  });

  $(".button2").click(function(event){
    event.stopPropagation();
    if($(".section").css("display") == "none"){
        $(".section").css("display","block");
    }else{
        $(".section").css("display","none");
    }
  });

  $(document).click(function(){
    $(".div").css("display","none");
    $(".section").css("display","none");
  });

});

The easiest way would be to start .click functions for both button 1 & 2 with: 最简单的方法是使用以下命令为按钮1和2启动.click功能:

.css("display","none");

function hidding the other element, like this: 隐藏其他元素的函数,如下所示:

  $(".button1").click(function(event){

    $(".section").css("display","none");

    event.stopPropagation();
    if($(".div").css("display") == "none"){
        $(".div").css("display","block");
    }else{
        $(".div").css("display","none");
    }
  });

  $(".button2").click(function(event){

    $(".div").css("display","none");

    event.stopPropagation();
    if($(".section").css("display") == "none"){
        $(".section").css("display","block");
    }else{
        $(".section").css("display","none");
    }
  });

Solution without JQuery... But you can use it if you really want to. 没有JQuery的解决方案...但是,如果您确实愿意,可以使用它。

 const $ = document.querySelector.bind(document); const $$ = document.querySelectorAll.bind(document); $('.button1').addEventListener('click', ()=>{ toggle('div') }); $('.button2').addEventListener('click', ()=>{ toggle('section') }); function toggle(element){ if($('.show')){ Array.from($$('.show')).forEach((ele) => { ele.classList.remove('show'); }); } $(element).classList.add('show'); } 
 html, body { background-color: #fafafa; width: 100%; height: 100%; padding: 12px; margin: 0; } .button1 { background-color: pink; position: relative; display: inline-block; padding: 8px; border: none; margin: 0 0 6px 0; cursor: pointer; } .div { background-color: hotpink; display: none; width: 160px; height: 160px; } .button2 { background-color: green; position: relative; display: inline-block; padding: 8px; color: white; border: none; margin: 0 0 6px 0; cursor: pointer; } .section { background-color: darkgreen; display: none; width: 160px; height: 160px; color: white; } .show { display: block; } 
 <button class="button1"> Show Div Element </button> <div class="div"> I am Div Element </div> <br> <br> <button class="button2"> Show Section Element </button> <section class="section"> I am Section Element </section> 

This solution relies on event bubbling, so it might break if other handlers stop propagation. 此解决方案依赖于事件冒泡,因此如果其他处理程序停止传播,则可能会中断。

Also note that it currently relies on provided html structure, but that can be tweaked easy enough. 还要注意,它当前依赖于提供的html结构,但是可以很容易地对其进行调整。

 $(document).click(function(e) { $elem = $(e.target) // If the element is visible we shouldn't open it again needToggle = $elem.is('button') && !$elem.next().hasClass("open") $(".open").removeClass("open") // Remove all open elements if (needToggle) { $elem.next().toggleClass("open") } }) 
 html, body { background-color: #fafafa; width: 100%; height: 100%; padding: 12px; margin: 0; } .button1 { background-color: pink; position: relative; display: inline-block; padding: 8px; border: none; margin: 0 0 6px 0; cursor: pointer; } .div { background-color: hotpink; display: none; width: 160px; height: 160px; } .button2 { background-color: green; position: relative; display: inline-block; padding: 8px; color: white; border: none; margin: 0 0 6px 0; cursor: pointer; } .section { background-color: darkgreen; display: none; width: 160px; height: 160px; color: white; } .open { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="button1"> Show Div Element </button> <div class="div"> I am Div Element </div> <br><br> <button class="button2"> Show Section Element </button> <section class="section"> I am Section Element </section> 

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

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