简体   繁体   English

单击按钮时重新加载iframe内容(本地HTML)

[英]Reload iframe content (local HTML) when clicking buttons

I´m loading HTML file into a DIV (iframe) when user click on a button. 当用户单击按钮时,我正在将HTML文件加载到DIV(iframe)中。

My problem is when click another button and need to reload the iframe with another HTML file. 我的问题是,当单击另一个按钮并需要用另一个HTML文件重新加载iframe时。

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? 如何在将下一个HTML加载到其中之前删除iframe内容?

You made a mistake here. 您在这里犯了一个错误。 An ID must not start with a number. ID不能以数字开头。 So use btn2015 instead of 2015 as ID on the button. 因此,使用btn2015代替2015作为按钮上的ID。

The link should do the rest: 该链接应完成其余工作:

Reload an iframe with jQuery 用jQuery重新加载iframe

Thanks Jonathan. 谢谢乔纳森。

Changing the ID and minor changes is working fine. 更改ID和较小的更改可以正常工作。

I´m testing another way and is working too: 我正在测试另一种方法,并且也在工作:

  1. Assign a "name" to the iframe with the default HTML: 使用默认HTML为iframe分配一个“名称”:

     <iframe name="year" src="2015.html"></iframe> 
  2. Direct <a> link with the URL and iframe "name" as target: 使用URL和iframe“名称”作为目标的直接<a>链接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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