2014-11-21 67 views
0

下面的代碼有效,但onclick只在第二次點擊按鈕後才起作用。爲什麼以及我需要做些什麼才能讓按鈕在第一次點擊時隱藏文字區域?按鈕的「第一次點擊」不隱藏div

我的javascript:

function hideShow() {           
    var e = document.getElementById('divHR'); 
    if(e.style.display == 'block') 
     e.style.display = 'none'; 
    else 
     e.style.display = 'block'; 

} 

HTML

<button id="btnHideShow" onclick="hideShow();"> 
    <img src="Images/arrow1.bmp" alt="Right Arrow icon" style="width:13px; 
     height:13px; border:0px;" /> 
     Hide or show Human Resources Information 
</button> 
<div id="divHR" class="showHRInfo"> 
    <h3 id="h3Inline">About Windsurf Human Resource (HR) division </h3> 
    <p id="pWindSurfHR" > Windsurf values and respects its employees very highly. 
     Should you have any problem, questions or concerns please 
     contact our Human Resource division. They are always at your service. 
    </p> 
</div> 
+0

'console.log(e.style.display);'將回答爲什麼 – epascarello 2014-11-21 18:28:54

+1

http://stackoverflow.com/questions/2880957/detect-inline-block-type-of-a-dom-element – epascarello 2014-11-21 18:30:50

+1

控制檯。如果您知道在瀏覽器中查找日誌的位置,日誌將會回答。在firefox上安裝firebug,然後使用F12鍵檢查它。 – 2014-11-21 18:32:40

回答

2

它應該工作,

function hideShow() {           
     var e = document.getElementById('divHR'); 
     if(e.style.display != 'none') 
      e.style.display = 'none'; 
     else 
      e.style.display = 'block'; 

    } 

你格樣式顯示屬性可能不會設置 '塊'。如果是這樣,那麼你的代碼集會阻止你的顯示屬性。

相關問題