简体   繁体   English

按钮在第一次点击时没有响应

[英]Button doesn't respond on first click

I have a button in the header of my website and it has to open/change another div display state, on first click. 我的网站标题中有一个按钮,必须在第一次单击时打开/更改另一个div显示状态。 But it's not responding on first click and I can't find the bug. 但是它在第一次点击时没有响应,我也找不到该错误。 I would appreciate some help.. 我将不胜感激。

 /* show search div */ function ShowSearchDiv() { var searchbox = document.getElementById("header-search-form"); if (searchbox.style.display === "none") { searchbox.style.display = "block"; document.getElementById("search-icon").classList.remove('fa-search', 'fa-lg2'); document.getElementById("search-icon").classList.add('fa-close', 'fa-lg-sm'); } else { searchbox.style.display = "none"; document.getElementById("search-icon").classList.remove('fa-close', 'fa-lg-sm'); document.getElementById("search-icon").classList.add('fa-search', 'fa-lg2'); } } 
 <button class="btn-rd-nav" onclick="ShowSearchDiv()" id="btn-search-home"> <i id="search-icon" class="fa-search fa fa-lg3 fa-bg"></i> </button> <div id="header-search-form" class="search-form slideInUpbox"> <!-----> <form method="post" action="search-results.asp?action=kword&keyw=&pg=" id="frmsk" name="frmsk" class="home-search"> <div class="input-group"> <input type="text" id="txtsearch" name="txtsearch" placeholder="What are you looking for?"> <button onClick="javascript:gosearch()" class="btn-search" type="submit"><span class="fa-search fa fa-lg"></span></button> </div> </form> </div> 

I found the bug and changed the if statement to: 我发现了该错误,并将if语句更改为:

 <script> /* show search div */ function ShowSearchDiv() { var searchbox = document.getElementById("header-search-form"); if (searchbox.style.display === "block") { searchbox.style.display = "none"; document.getElementById("search-icon").classList.remove('fa-close', 'fa-lg-sm'); document.getElementById("search-icon").classList.add('fa-search', 'fa-lg2'); } else { searchbox.style.display = "block"; document.getElementById("search-icon").classList.remove('fa-search', 'fa-lg2'); document.getElementById("search-icon").classList.add('fa-close', 'fa-lg-sm'); } } </script> 

This way instead of checking "if the div is hidden, then make it visible and else hide it...", I changed it to "if the div is visible, then make it hidden, else leave it visible :) Now it's working on first click. 这样,我将其更改为“如果div可见,则将其隐藏,否则将其保留为可见:),而不是检查“是否div被隐藏,然后使其可见,然后将其隐藏...”,而不是检查它。第一次点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM