2014-12-07 39 views
0

第二次點擊切換按鈕後,一切正常工作。有些行可以像第一次點擊時那樣工作,所以我知道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>") 
+0

'document.getElementById('camimg'+ cnt);'元素是否有顯示值?如果沒有,一旦設置,你會看到if/else正常工作。 – rfornal 2014-12-07 23:26:17

+0

最初顯示帶有id('camimg'+ cnt)的圖像。然後,我必須點擊該切換按鈕兩次才能隱藏這些圖像並正確顯示其後正確運行的圖像。從一開始,我必須點擊切換按鈕TWICE。但隨後可以使用單擊事件進行工作/切換。 – 2014-12-07 23:53:03

回答

0

嘗試顯式設置顯示,這是風格的顯示器,而不管它是否顯示。 行之後所以:

retstr.AppendLine( 「類= '項目' 」)

添加下列行

retstr.AppendLine(「 風格= '顯示:塊'」)

相關問題