简体   繁体   English

在隐藏和显示元素之间切换

[英]toggle between hiding and showing an element

Im learning how to program and I would like to toggle between hiding and showing an element with JavaScript. 我正在学习如何编程,我想在隐藏和显示JavaScript元素之间切换。 The script is shown below. 该脚本如下所示。 It works but instead of showing the solution once the program runs, I would like to have it hidden so that the user can click and see the solution. 它可以工作,但是我不想将其隐藏在程序运行后显示解决方案,而是希望将其隐藏起来,以便用户单击并查看解决方案。 I would like to have it the other way round. 我想反过来。 I've tried with 'display:none' but it stops working. 我已经尝试过使用'display:none',但是它停止工作。 I know its easy but I cant make it work. 我知道它很简单,但是我无法使其工作。 Can you help me? 你能帮助我吗?

 function SolutionFunction() { var x = document.getElementById("solution"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } 
 #solution { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; } 
 <button onclick=" SolutionFunction ()">Try it</button> <div id=" solution "> This is the solution. </div> 

On default, the display parameter is empty, when you get it with x.style.display , because this only look on element properties, and not the CSS styles. 默认情况下,使用x.style.display获取时, display参数为空,因为它仅查看元素属性,而不查看CSS样式。 So you could check if it is empty. 因此,您可以检查它是否为空。

And you need to remove the spaces around the id to make it working. 并且您需要删除id周围的空格以使其正常运行。

 function SolutionFunction() { var x = document.getElementById("solution"); if (x.style.display === "") { x.style.display = "block"; } else { x.style.display = ""; } } 
 #solution { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; display: none; } 
 <button onclick=" SolutionFunction ()">Try it</button> <div id="solution"> This is the solution. </div> 

Or you put the display: none to the style attribute of the element, and your code will work too: 或者,您将display: none放到元素的style属性上,您的代码也将起作用:

 function SolutionFunction() { var x = document.getElementById("solution"); if (x.style.display === "none") { x.style.display = "block"; } else { x.style.display = "none"; } } 
 #solution { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; } 
 <button onclick=" SolutionFunction ()">Try it</button> <div id="solution" style="display: none;"> This is the solution. </div> 

You can use the hidden attribute/property on the element to toggle the visibility instead. 您可以使用元素上的隐藏属性/属性来切换可见性。

You should also remove any spaces you have in id="" to be able to properly get the element by id 您还应该删除id=""中的所有空格,以便能够通过id正确获取元素

 function SolutionFunction() { var x = document.getElementById("solution"); x.hidden = !x.hidden } 
 #solution { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; } 
 <button onclick=" SolutionFunction ()">Try it</button> <div id="solution" hidden> <!-- hidden by default --> This is the solution. </div> 

Using the hidden = true|false method to toggle the visibility is a better approach since you don't always know how a element display property should be handled after you reset the display style. 使用hidden = true|false方法切换可见性是一种更好的方法,因为在重置display样式后,您并不总是知道如何处理元素显示属性。 You might want to have the element rendered as inline-block or if it's a <tr> it should be rendered as row and not a block 您可能希望将元素呈现为inline-block或者如果它是<tr> ,则应呈现为行而不是块


Another possible solution if you will is the <details> element but it don't work in Internet explorer or Edge yet. 另一个可能的解决方案是<details>元素,但是它在Internet Explorer或Edge中尚不起作用。 but it looks simular to what you try to accomplish and don't require any javascript. 但它看起来与您要完成的工作类似,不需要任何JavaScript。

See browser support here https://caniuse.com/#feat=details 在此处查看浏览器支持https://caniuse.com/#feat=details

 #solution { width: 100%; padding: 50px 0; text-align: center; background-color: lightblue; margin-top: 20px; } 
 <details> <!-- can use the open attribute to show it by default --> <summary>try it</summary> <div id="solution"> this is the solution </div> </details> 

it might not look as pretty but it's possible to style it more 它可能看起来不那么漂亮,但可以对其进行更多样式设置

Stop the spaces! 停止空间!

function SolutionFunction() {
  var x = document.getElementById("solution");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
#solution {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}
<button onclick="SolutionFunction()">Try it</button>

<div id="solution" style="display: none;">
  This is the solution.
</div>

Better yet, reference the element in your function as an argument. 更好的是,将函数中的元素作为参数引用。 Also look into ternary operators: 还要研究三元运算符:

   function SolutionFunction(x) {
      x.style.display = x.style.display === "none" ? "block" : "none";
    }
    #solution {
      width: 100%;
      padding: 50px 0;
      text-align: center;
      background-color: lightblue;
      margin-top: 20px;
    }
    <button onclick="SolutionFunction(document.getElementById('solution'))">Try it</button>

    <div id="solution" style="display: none;">
      This is the solution.
    </div>

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

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