简体   繁体   中英

How to add a listener with JavaScript

I'm developing a page in Chrome and I want it to show a modal when the user clicks an image but when I try it with my localhost it does nothing. I think there's a problema with a listener. This is my code:

$(document).ready(function() {

  document.getElementByClass(".call-to-action").addEventListener("click", openModal());

});

function openModal() {
  $("html").addClass("modal-open");
}

My CSS:

.modal-open
  .modal
    opacity: 1
    pointer-events: all

Thanks!!

This might help you, cheers!

If you are using Jquery :

 $(document).ready(function() { $(".call-to-action").on("click", openModal); }); function openModal() { $("html").addClass("modal-open"); } 
 html.modal-open{ background: #ddd; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body> <input class="call-to-action" type="button" value="CTA"/> </body> 

If you are using javascript :

 document.getElementsByClassName("call-to-action")[0].addEventListener("click", openModal); function openModal() { document.documentElement.classList.add("modal-open"); } 
 html.modal-open { background: #ddd; } 
 <body> <input class="call-to-action" type="button" value="CTA" /> </body> 

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