奇怪的是,在第二次单击切换隐藏/显示按钮而不是第一次单击后,有两行有效

Strangely, two lines work after SECOND click of toggle hide/show button not FIRST click

本文关键字:单击 第一次 有效 两行 显示 第二次 隐藏 按钮      更新时间:2023-09-26
第二次

单击切换按钮后,一切都可以完美运行。有些行在第一次单击时正常工作,因此我知道正在输入 if 和 else 内容。请参阅代码注释以更好地理解。我的逻辑哪里有问题,导致这两行在第一次点击后不起作用?即使您不确切知道问题所在,也希望了解可能导致此问题的任何想法。

   function toggleView(switchImgTag) {
        var cnt = 0;
        var but = document.getElementById("but_Toggle");
        while (cnt <= 5000) { 
            window['ele' + cnt] = document.getElementById('camimg' + cnt);
            window['imageEle' + cnt] = document.getElementById('camimg' + cnt);
            if (window['ele' + cnt].style.display == "block") {
             but.innerHTML = "Thumbs"; //Strangely, executes after "second" click of List button
             window['ele' + cnt].style.display = "none"; //Strangely, executes after "second" click of List button
             document.getElementById('im' + cnt).style.width = '20px'; //Executes after very first click of List button as it should
             document.getElementById('im' + cnt).style.visibility = 'visible'; //Executes after very first click of List button as it should
         }
         else { 
             but.innerHTML = "List";
             window['ele' + cnt].style.display = "block";
             document.getElementById('im' + cnt).style.width = '1px'; //Executes after first click of List button as it should
             document.getElementById('im' + cnt).style.visibility = 'hidden'; //Executes after very first click of List button as it should
         }
         cnt++;
        }
    }

然后我有按钮..

<button id="but_Toggle" type="button" onclick="javascript:toggleView();">List</button>

然后我控制了内容...

 retstr.AppendLine("<img alt ='" & streamref & "'")
    retstr.AppendLine(" title ='" & Trim(image_title_with_PTZ) & " - " & streamref & "'")
    retstr.AppendLine(" src = '" & imgsrc & "'")
    retstr.AppendLine(" class = 'item'")
    If searchbar = False Then
        retstr.AppendLine(" id = 'camimg" & count & "'")
    Else
        retstr.AppendLine(" id = 'camimg" & count & "'")
    End If
    retstr.AppendLine(" ondblclick = ""javascript:window.open('" & imgsrc & "')""")
    'retstr.AppendLine("  data-streamurl='" & "rtsp://127.0.0.1/cam.stream" & "'")
    retstr.Append("/></div>")
    retstr.AppendLine("<div class='preview'")
    retstr.AppendLine(" title ='" & Trim(image_title_with_PTZ) & " - " & streamref & "'")
    retstr.AppendLine(" id = '" & imgsrc & "'")
    retstr.AppendLine(">")
    retstr.Append("<div class=imagename><img alt ='" & streamref & "' title ='" & Trim(image_title_with_PTZ) & " - " & streamref & "'" & " id='im" & count & "' src='../images/cam.png' height=20 class=item2  ondblclick =javascript:window.open('" & imgsrc & "')><font size=1><a href='" & imgsrc & "'>" & image_title_with_PTZ & "</a></font></nb></span></div></div>")

尝试显式设置显示,这是样式的显示,而不是是否显示。所以在行之后:

雷斯特。AppendLine(" class = 'item'")

添加以下行

雷斯特。AppendLine(" style = 'display:block'")