简体   繁体   中英

How to prevent a Javascript click event from happening twice?

I had a big problem in getting a Google maps iframe to load on center, inside a twitter bootstrap modal. The solution that I found to work is loading the iframe by javascript, once the modal is opened. The problem that I have with my code at the moment is that once the modal is closed and re-opened, the iframe will be unloaded. So the question is how to prevent a click event from happening twice?

My code:

<script type="text/javascript"> 
$(function() {
$("#map_link").click(function(event) {  
    event.preventDefault();
    $("#map").slideToggle(); 
    $("#map").html('Iframe_code_is_situated_here').css('display','block');
    });  
});
</script>
$("#map_link").one('click', function(event) {  

Keep track of whether or not it has been clicked and return false if it has been clicked

 $(function() {
    var click_limit = 1, clicks = 0;

    $("#map_link").click(function(event) {  
       if (clicks++ === click_limit){ return false; }
       event.preventDefault();
       $("#map").slideToggle(); 
       $("#map").html('Iframe_code_is_situated_here').css('display','block');
    });  
});

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