简体   繁体   中英

onclick event don't work on WEB page

I want to create a modal box. It should appear, when a user clicks a button, and close, when the user clicks a close button in the modal box. I made two functions:

  • check() : it change css. After it's elememt onclick, appears modal box
  • close() : this function should close modal box, but it doesm't. It simply should set previous css setting. This function isn't execution at all when I click a close button(span element).

Why close function doesn't work and what I should modernize to get a result?

Thanks in advance)

 function check() { var modal = document.getElementById("myModal"); modal.style.display = "block"; } //this function dont't work function close(){ var modal = document.getElementById("myModal"); modal.style.display = "none"; } 
 .modal { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); } .modal-content { background-color: #fefefe; margin: auto; padding: 20px; border: 1px solid #888; width: 80%; } 
 <html> <body> <button id="button" type="submit" onclick="check()">Login</button> <div id="myModal" class="modal"> <div class="modal-content" > <span class="close" onclick="close()">&times;</span> <p >Some text in the Modal..</p> </div> </div> </body> </html> 

close is already the name of a native browser function, window.close() . Rename your function and it works fine:

 function check() { var modal = document.getElementById("myModal"); modal.style.display = "block"; } function closeModal(){ var modal = document.getElementById("myModal"); modal.style.display = "none"; } 
 .modal { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); } .modal-content { background-color: #fefefe; margin: auto; padding: 20px; border: 1px solid #888; width: 80%; } 
 <html> <body> <button id="button" type="submit" onclick="check()">Login</button> <div id="myModal" class="modal"> <div class="modal-content" > <span class="close" onclick="closeModal()">&times;</span> <p >Some text in the Modal..</p> </div> </div> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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