简体   繁体   English

如何显示显示:无DIV使用Javascript

[英]How to show a display:none DIV using Javascript

I have a DIV which has a display set to none, by using javascript I tried showing it by using the onclick of a button. 我有一个显示设置为无的DIV,通过使用javascript我尝试通过使用按钮的onclick显示它。 But what happens is the exact opposite. 但恰恰相反,会发生什么。 My DIV is already shown and when I click the button it hides the DIV. 我的DIV已经显示,当我点击按钮时,它隐藏了DIV。 What am i doing wrong here, please HELP! 我在这做错了什么,请帮忙!

This is my button and the div: 这是我的按钮和div:

<button onclick="myFunction()">SHOW</button>
    <div id="how_to_form">
      <img src="../images/view.png">
    </div>

This is the JS code: 这是JS代码:

function myFunction() {
     var x = document.getElementById("how_to_form").style.display = 'none';
     if (x.style.display === "none") {
      x.style.display = "block";
    } else {
      x.style.display = "none";
    }
  }

have you checked if you have already set a default style to your div? 你有没有检查过你的div是否已经设置了默认样式? you either have to set your div's default style to display:none by inline 您必须将div的默认样式设置为显示:无内联

<div id="how_to_form" style="display:none">

or by css 或者通过CSS

<style>
    #how_to_form{ display:none }
</style>

Your line there has a double assignment: 你的线路有双重任务:

var x = document.getElementById("how_to_form").style.display = 'none';

It first assigns the display to none: 它首先将display分配给none:

document.getElementById("how_to_form").style.display = 'none';

and then takes the result of that expression (which is the string you assigned), and assigns it to x : 然后获取该表达式的结果(这是您指定的字符串),并将其分配给x

var x = 'none';

Which isn't what you want. 这不是你想要的。 First declare the variable for the element, then assign its style. 首先声明元素的变量,然后分配它的样式。

Also, it sounds like you want the element to start out hidden - assign its initial style outside the function: 此外,听起来您希望元素开始隐藏 - 在函数外部分配其初始样式:

const form = document.getElementById("how_to_form");
form.style.display = 'none';
function myFunction() {
  if (form.style.display === "none") {
    form.style.display = "block";
  } else {
    form.style.display = "none";
  }
}

Or, to be more concise, use the conditional operator: 或者,更简洁,使用条件运算符:

function myFunction() {
  form.style.display = form.style.display === "none"
  ? 'block'
  : 'none';
}

Also consider attaching the handler properly using Javascript, rather than using inline HTML attributes, which are generally considered to be pretty poor practice and can be hard to manage: 还要考虑使用Javascript正确附加处理程序,而不是使用内联HTML属性,这些属性通常被认为是非常糟糕的做法,并且可能难以管理:

document.querySelector('button').addEventListener('click', myFunction);

Issue is your variable assignment 问题是你的变量赋值

Chaining the assignment operator is possible in order to assign a single value to multiple variables. 链接赋值运算符是可能的,以便为多个变量分配单个值。

Please refer this link for variable assignment options https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators 请参阅此链接以获取变量分配选项https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators

var x = y= 10 var x = y = 10

Then both x and y values are 10 然后x和y值都是10

Similarly in your code var x is none 同样在你的代码中var x是none

To achieve expected result , use below option 要达到预期效果,请使用以下选项
1. set CSS for html_to_form to display:none 2.In your code change variable x assignment to var x = document.getElementById("html_to_form") 1.为html_to_form设置CSS以显示:none 2.在您的代码中更改变量x赋值给var x = document.getElementById(“html_to_form”)

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

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