简体   繁体   中英

Reload iframe content (local HTML) when clicking buttons

I´m loading HTML file into a DIV (iframe) when user click on a button.

My problem is when click another button and need to reload the iframe with another HTML file.

This is my code:

<script type="text/javascript">
$(function(){
    $('#2015').click(function(){
        if(!$('#iframe').length) {
                $('#content').html('<iframe id="iframe" src="2015.html" width="700" height="450"></iframe>');
        }
    });
});
</script>

And here is the button code:

<a href ="#" id="2015">2015</a>

How can I remove the iframe content before to load the next HTML into it?

You made a mistake here. An ID must not start with a number. So use btn2015 instead of 2015 as ID on the button.

The link should do the rest:

Reload an iframe with jQuery

Thanks Jonathan.

Changing the ID and minor changes is working fine.

I´m testing another way and is working too:

  1. Assign a "name" to the iframe with the default HTML:

     <iframe name="year" src="2015.html"></iframe> 
  2. Direct <a> link with the URL and iframe "name" as target:

     <a href="2014.html" target="year">Change Link</a> 

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