首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按钮的“第一次点击”不隐藏div

按钮的“第一次点击”不隐藏div
EN

Stack Overflow用户
提问于 2014-11-21 18:24:15
回答 1查看 864关注 0票数 0

下面的代码可以工作,但是onclick只在第二次单击按钮之后才能使用该函数。为什么和我需要做什么按钮隐藏文本区域的第一次点击?

我的javascript:

代码语言:javascript
复制
function hideShow() {                                         
   var e = document.getElementById('divHR');
   if(e.style.display == 'block')
       e.style.display = 'none';
   else
       e.style.display = 'block';

 }

html

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-21 19:05:12

应该管用的,

代码语言:javascript
复制
 function hideShow() {                                         
       var e = document.getElementById('divHR');
       if(e.style.display != 'none')
           e.style.display = 'none';
       else
           e.style.display = 'block';

     }

您的div样式显示属性可能不会设置“块”。如果是这样,那么您的代码集就会阻塞display属性。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27067835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档