奇怪的是,在第二次点击切换隐藏/显示按钮而不是第一次点击后,两条线工作

时间:2014-12-07 23:22:08

标签: javascript jquery ajax toggle

在第二次点击切换按钮后,一切正常。有些行在FIRST点击时应该正常工作,所以我知道输入的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>")

1 个答案:

答案 0 :(得分:0)

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

retstr.AppendLine(&#34; class =&#39; item&#39;&#34;)

添加以下行

retstr.AppendLine(&#34; style =&#39; display:block&#39;&#34;)