简体   繁体   English

单击按钮时重叠式div无法正常工作

[英]Overlay div is not working when I click the button

I'm having a problem opening an overlay div when I click on the button. 单击按钮时,打开重叠式div时出现问题。 I'm using CSS to create the overlay and then jQuery to display the div. 我正在使用CSS创建叠加层,然后使用jQuery显示div。 When I click on the button the overlay does not open. 当我单击按钮时,覆盖层不会打开。

What I'm doing wrong? 我做错了什么?

 function openSearch() { document.getElementById("overlay-search").style.display = "block"; } function closeSearch() { document.getElementById("overlay-search").style.display = "none"; } 
 .overlay-s-style { position: fixed; display: none; width: 100%; height: 100%; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); } 
 <div id="overlay-search" class="overlay-s-style"> <div class="search-block"> </div> </div> 

You have the functions but you aren't calling them anywhere. 您具有这些功能,但没有在任何地方调用它们。 Add an event listener to the button so when it gets clicked, a function get called. 向按钮添加事件侦听器,以便在单击按钮时调用函数。 Add in a case or an if statement to check if the overlay is displayed so that it knows which function to call. 在case或if语句中添加以检查是否显示了覆盖图,以便它知道要调用哪个函数。

It works, as long as you are calling the functions. 只要您调用函数,它就可以工作。

I fiddled it together to make it clear. 我把它弄清楚了。 As already pointed out, there is no jQuery involved here, did you provide all your code? 如前所述,这里不涉及jQuery,您是否提供了所有代码?

 function openSearch() { document.getElementById("overlay-search").style.display = "block"; } function closeSearch() { document.getElementById("overlay-search").style.display = "none"; } var openBtn = document.getElementById('open'); openBtn.addEventListener('click', function(ev) { openSearch(); }); var closeBtn = document.getElementById('close'); closeBtn.addEventListener('click', function(ev) { closeSearch(); }); 
 .overlay-s-style { position: fixed; display: none; width: 100%; height: 100%; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="overlay-search" class="overlay-s-style"> <div class="search-block"> </div> <button id="close">close</button> </div> <button id="open">open</button> 

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

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