简体   繁体   English

单击同一个按钮时如何显示或隐藏div?

[英]How to show or hide a div when clicking on the same button?

I face a problem when i want to add interactivity on my leaflet map.当我想在我的 leaflet map 上添加交互性时,我遇到了一个问题。

I have a button on my map我的 map 上有一个按钮

<button id="az">Availability Zones</button>

The thing i want is when i click on it, it show a square of informations on my map So i have create a square我想要的是当我点击它时,它会在我的 map 上显示一个正方形的信息所以我创建了一个正方形

<div class="square" id='square'> </div> 
CSS = .square{
  z-index: 4000;
   width: 100%;
  height: 0;
  padding-top: 100%;
  background-color: #ccc;
  position: absolute;
  display: none;
}

And an other css class square with same properties but with a display: block和另一个具有相同属性但带有显示的 css class 正方形:块

.squareclick{
  z-index: 4000;
   width: 30%;
  weight: 30%;
  padding: 0 25 30;
  margin-left: 400px;
  margin-bottom: 200px;
  height: 0;
  padding-top: 100%;
  background-color: #ccc;
  position: relative;
  display: block;
}

Now, for opening this square on a button i've add some interactivity现在,为了在按钮上打开这个正方形,我添加了一些交互性

var button = document.getElementById('az')
L.DomEvent.on(button,'click',function(e){
    console.log('Button clicked')
});

L.DomEvent.on(button,'click',function(){
    document.getElementById('square').setAttribute("class", "squareclick");
  
});

The thing is that that button works for opening the square, but not for closing (I know this is normal) I've try that thing but it seems to not work问题是该按钮可用于打开正方形,但不适用于关闭(我知道这是正常的)我已经尝试过,但它似乎不起作用

L.DomEvent.on(button,'click',function myFunction() {
  var x = document.getElementById("square");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }

I don't know how to add a second interactivity on the same button:(我不知道如何在同一个按钮上添加第二个交互性:(

If someone can help!如果有人可以帮忙! Thank you very much非常感谢

What you can try doing is instead of directly checking whether the square is visible or not, you can set a variable to check.您可以尝试做的不是直接检查正方形是否可见,而是可以设置一个变量来检查。 Change:改变:

L.DomEvent.on(button,'click',function myFunction() {
  var x = document.getElementById("square");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

To:至:

var shown = false;    

L.DomEvent.on(button,'click',function myFunction() {
  var x = document.getElementById("square");
  if (shown == false) {
    x.style.display = "block";
    shown = true;
  } else if (shown == true) {
    x.style.display = "none";
    shown = false;
  }
}

The variable shown tells us at the start the square is not visible. shown的变量一开始就告诉我们正方形是不可见的。 Every time you click the button, the variable changes and so does the style.每次单击按钮时,变量都会发生变化,样式也会发生变化。 If the square was to be visible at the start, then you can simply change shown to true at the start of the script.如果正方形在开始时可见,那么您只需在脚本开始时将shown更改为 true。 See if that way works.看看这种方式是否有效。 :) :)

I recommand to only add a class to the square that changes the display: none style.我建议只将 class 添加到更改display: none样式。

var button = document.getElementById('az')
var square = document.getElementById('square')
L.DomEvent.on(button,'click',function(e){
    console.log('Button clicked')
    if(L.DomUtil.hasClass(square,'show')){
        L.DomUtil.removeClass(square,'show');
    }else{
        L.DomUtil.addClass(square,'show');
    }
});
.square{
  z-index: 4000;
   width: 30%;
  weight: 30%;
  padding: 0 25 30;
  margin-left: 400px;
  margin-bottom: 200px;
  height: 0;
  padding-top: 100%;
  background-color: #ccc;
  position: absolute;
  display: none;
}

.show{
  display: block;
}

https://jsfiddle.net/falkedesign/v8tdzmhe/ https://jsfiddle.net/falkedesign/v8tdzmhe/

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

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