[英]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">℉</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.