【问题标题】:How to refresh 2 iframes with one link/button using html/js如何使用 html/js 通过一个链接/按钮刷新 2 个 iframe
【发布时间】:2011-07-09 21:33:19
【问题描述】:

我是新来的,想知道如何在一个页面上刷新 2 个不同的 iframe。

我使用 getElemenById 在 google 上找到了一些东西。但是它必须在firefox中工作,并且firefox的Id有一些问题。

提前致谢。

<form action="managecartform.html" onclick="deleteAllCookies();"><button type="submit" >Empty cart</button></form>

【问题讨论】:

  • 请用您拥有的实际 html 更新您的 html,并指定何时重新加载 iframe 以及是否要将表单提交到当前页面

标签: javascript html iframe refresh


【解决方案1】:

您的表单与 iframe 有什么关系?

你是这个意思吗?将 managecartform 加载到一帧并重新加载另一帧?

<form action="managecartform.html" target="iframe1" 
onsubmit="deleteAllCookies(); window.frames[0].location.reload(1);">
<input type="submit" value="Empty cart"/>
</form>
<iframe name="iframe0"></iframe>
<iframe name="iframe1"></iframe>

【讨论】:

    【解决方案2】:

    firefox 没有 id 问题 - 99% 的时间是因为您丢失了 id 或者您复制了一个 id。

    id 在整个文档中必须是唯一的。

    回答你的问题:

    <iframe id="frame1"></iframe>
    <iframe id="frame2"></iframe>
    
    <input type="button" onclick="refreshFrames()" value="refresh frames" />
    
    <script type="text/javascript">
         function refreshFrames(){
              frame1 = document.getElementById('frame1');
              frame2 = document.getElementById('frame2');
              if(frame1.contentDocument){
                  frame1.contentDocument.location.reload(true);
                  frame2.contentDocument.location.reload(true);
              } else {
                  frame1.contentWindow.location.reload(true);
                  frame2.contentWindow.location.reload(true);
              }
         }
    </script>
    

    (对于 IE,您可能必须使用 contentWindow 而不是 contentDocument,具体取决于您尝试支持的 IE 版本)

    【讨论】:

    • 让我重新表述第一部分:在我从事客户端开发的 10 年中,我从未遇到过 document.getElementById() 在 Firefox 中不起作用的问题,除非我要么缺少元素上的 id 属性,要么有两个具有相同 id 属性的项目。
    • 我尝试了您的代码,但是当我从 Firefox 打开错误控制台时,我收到消息:frame1 = null。 iframe 编码如下:
    猜你喜欢
    • 2011-08-15
    • 1970-01-01
    • 2010-09-29
    • 2021-08-02
    • 2019-11-29
    • 2023-03-29
    • 2010-10-17
    • 2015-10-10
    • 2018-02-23
    相关资源
    最近更新 更多