简体   繁体   中英

JavaScript on click event with an image src

I'm learning to code and have a question for which I couldn't find an answer so far. I'm trying to create an on click event (anywhere on the page) and have an image from my folder pop up at the bottom of the page (absolutely positioned). Bonus: I'd love to find a way for this to trigger every 5th click. Sounds like a potentially annoying concept functionality-wise, but I need it for this particular project. Would anybody be so kind to assist me with this?

Try below solution which will alert on 5th click on page.

 var Constants = {}; $(document).ready(function() { Constants.clickCount = 0; $(document).on('click', function(){ Constants.clickCount = Constants.clickCount +1; if(Constants.clickCount == 5) { alert("5th Time Click"); Constants.clickCount = 0; } }) });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body style="background-color:red; height:500px;width:500px;"> Start Clicking on Page </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