【问题标题】:Auto Refresh IFrame HTML自动刷新 IFrame HTML
【发布时间】:2021-05-15 12:13:15
【问题描述】:

如何在不刷新整个页面的情况下每 3 秒自动刷新一次 iframe。我使用<meta http-equiv="refresh" content="1">,但它会显示整个页面刷新,并且每次都会将您置于页面顶部。我的 iframe 指向一个文本文件以读取我输入的实时消息。有没有办法做到这一点而不刷新整个页面,只刷新元素。我是 HTML 初学者,所以请彻底解释一下。我谷歌了很多,尝试了很多东西,到目前为止没有任何效果,我在很多浏览器中尝试。

到目前为止我的代码:

<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="favicon.png" type="image/x-icon" />



<title>Chat</title>

<style>

body{
margin: 0px;
}

h1{
font-family: arial;
font-size: 100%;
}
iframe{
width: 900px;
height: 500px;
}

div.top{
background-color: rgba(2, 109, 8, 0.83);
height: 30px;
width: 100%;
}
</style>


</head>

<body>
<div class="top">
<img src="favicon.png" alt="favicon"  height="31" width="31" >
<h1>Chat</h1>
</div>




<iframe src="text.txt" name="iframe_a"> />



</body>
</html>

【问题讨论】:

标签: html iframe automation refresh element


【解决方案1】:

您的 html 标签有问题。 (“iframe”标签格式错误)

在这里,您已修复代码并准备运行答案:

<!DOCTYPE html>
<html>
<head>
    <link rel="shortcut icon" href="favicon.png" type="image/x-icon" />
    <title>Chat</title>
    <style>
        body {
            margin: 0px;
        }
        h1 {
            font-family: arial;
            font-size: 100%;
        }
        iframe {
            width: 900px;
            height: 500px;
        }
        div.top {
            background-color: rgba(2, 109, 8, 0.83);
            height: 30px;
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="top">
        <img src="favicon.png" alt="favicon" height="31" width="31">
        <h1>Chat</h1>
    </div>

    <iframe id="iframe" src="text.txt"></iframe>

    <script>
        window.setInterval(function() {
            reloadIFrame()
        }, 3000);

        function reloadIFrame() {
            console.log('reloading..');
            document.getElementById('iframe').contentWindow.location.reload();
        }
    </script>
</body>
</html>

问候

【讨论】:

    【解决方案2】:
    <head>
        <script>
            function refreshIFrame() {
                var x = document.getElementById("*Your_iframe_id*");
                x.contentWindow.location.reload();
                var t = setTimeout(refreshIFrame, 3000);
            }
        </script>
    </head>
    <body onload="refreshIFrame()">... 
        <iframe id="*Your_iframe_id*" src= ...></iframe>...
    </body>
    

    这个解决方案对我有用,我发现“refreshIFrame()”函数作为参数输入,没有大括号 () 在以下行中:“ var t = setTimeout(refreshIFrame, 3000);"。只需替换相关值即可。

    【讨论】:

      【解决方案3】:

      您只需使用几行 Javascript 即可完成您需要的工作。

       <script>
       window.setInterval("reloadIFrame();", 3000);
      
       function reloadIFrame() {
        document.frames["frameNameHere"].location.reload();
       }
       </script> 
      

      【讨论】:

      • 在 Firefox、Chrome 或 IE 中不起作用。
      • 我把它放进去,什么也没发生。
      • 你用你的框架名替换了吗?
      猜你喜欢
      • 2011-10-21
      • 2012-09-09
      • 2017-12-14
      • 2015-11-29
      • 2015-11-30
      • 2022-06-10
      • 2011-09-25
      • 2011-12-11
      相关资源
      最近更新 更多