简体   繁体   English

为什么这些div无法隐藏和显示?

[英]Why do these divs not get hidden and revealed?

I can't figure out why the following code will unhide the target elements, but won't hide them. 我不知道为什么下面的代码会取消隐藏目标元素,但不会隐藏它们。 The list msg is received from a websocket. 列表消息是从websocket接收的。 It is of the form: 它的形式为:

"Line, 4, Auto"
"Line, 4, Heat"
"Line, 4, Cool"
"Line, 4, Fan"
"Line, 4, Off"

After pushing the list into the array matrix, I use switch blocks to enter the switch block for array[2]. 将列表推入数组矩阵后,我使用切换块输入array [2]的切换块。 The code properly unhides the element when appropriate, but won't hide them. 代码会在适当的时候适当地取消隐藏元素,但不会隐藏它们。

 // dummy code var lines = `Line 4, Heat;Line, 4, Cool;Line, 4, Auto;Line, 4, Fan;Line, 4, Off`.split(";"); lines.forEach(function(msg) {HideUnhide(msg) }) function HideUnhide(msg) { // end dummy code var array = msg.split(','); alert(msg) switch (array[0]) { case "Line": switch (Number(array[1])) { case 0: document.getElementById("Scale").innerHTML = array[2]; break; case 4: document.getElementById("Control").innerHTML = array[2]; test = array[2]; switch (test.trim()) { case "Auto": document.getElementById("Hot").style.visibility = "visible"; document.getElementById("Cold").style.visibility = "visible"; break; case "Heat": document.getElementById("Hot").style.visibility = "visible"; document.getElementById("Cold").style.visibility = "invisible"; break; case "Cool": document.getElementById("Hot").style.visibility = "invisible"; document.getElementById("Cold").style.visibility = "visible"; break; default: document.getElementById("Hot").style.visibility = "invisible"; document.getElementById("Cold").style.visibility = "invisible"; break; } } } } 
 div.Hot { position: fixed; top: 75px; left: 24px; width: 100px; font: 20px Arial Bold; color: rgb(200, 200, 200); } div.Cold { position: fixed; top: 50px; left: 24px; width: 100px; font: 20px Arial Bold; color: rgb(200, 200, 200); } div.Control { position: fixed; top: 25px; left: 24px; width: 100px; font: 20px Arial Bold; color: rgb(200,200,200); } div.Scale { position: fixed; top: 0px; left: 24px; width: 100px; font: 20px Arial Bold; padding-right: 30px; color: rgb(200,200,200); } 
 <div id="Hot" style="visibility: hidden; color:rgb(200,200,200)" class="Hot">Cool: 70.0</div> <div id="Cold" style="visibility: hidden; color:rgb(200,200,200)" class="Cold">Heat: 64.0</div> <div ID="Scale" style="color:rgb(200,200,200)" class="Scale">&#8457;</div> <div ID="Control" style="color:black" class="Control">Cool</div> 

The array element you are passing is " Auto", where your switch statement is looking for "Auto", notice the white space. 您要传递的数组元素是“ Auto”,您的switch语句正在寻找“ Auto”,请注意空格。 You could change the way the message is created at origin, or you can check for white space and remove it, or edit your switch statement to accommodate for it. 您可以更改在原始位置创建消息的方式,也可以检查空白并删除它,或者编辑switch语句以适应它。 I would recommend removing the space at message creation if that is an option and does not create more work elsewhere. 如果可以选择在消息创建时建议删除空间,并且不会在其他地方创建更多工作。

I found the problem. 我发现了问题。 The syntax is: 语法为:

document.getElementById("Hot").style.visibility = "hidden";

not: 不:

document.getElementById("Hot").style.visibility = "invisible";

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

相关问题 为什么单击按钮时我的隐藏表没有显示? - Why is my hidden table not being revealed on button click? 当我有一个隐藏的字段(如果选择了一个选项时会显示)时,我无法获得select元素的值 - I can't get the value of my select element when i have a hidden field that is revealed if an option is selected 隐藏的导航栏在悬停时显示,但不是全宽 - Hidden Navigation Bar Revealed on Hover But Not Full Width 为什么模板 div 在 afterRender 中显示为“:hidden”? - Why are template divs showing as “:hidden” in afterRender? 为什么隐藏的div自动显示? - Why are my hidden divs auto appearing? 获取div的高度,全部位于隐藏的div中 - get height of divs, all in a hidden div 标签-需要内容在页面加载时隐藏并在单击时显示 - Tabs - need content to be hidden on page load and revealed when clicked 在内部或外部时如何隐藏或显示文本 <div> ? - How to make text hidden or revealed when inside or outside a <div>? Facebook开放图表 - &#39;喜欢按钮&#39; - 隐藏的内容透露在&#39;喜欢&#39;上 - Facebook Open Graph - 'Like Button' - Hidden Content Revealed Upon 'Like' 5个用户输入用于调查,所有输入都被隐藏,然后在最后一次输入被回答后才显示 - 5 user inputs for a survey, all hidden and then revealed after that last input is answered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM