下面的代码有效,但是onclick仅在第二次单击按钮后才可使用该功能。为什么在第一次单击该按钮时要隐藏文本区域,我需要怎么做?

我的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>

最佳答案

它应该工作,

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

     }


您的div样式显示属性可能未设置“阻止”。如果是这样,则您的代码集将阻塞您的显示属性。

关于javascript - Button的“首次单击”不会隐藏div,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27067835/

10-11 12:49