简体   繁体   English

使用JavaScript自动刷新div

[英]Auto refreshing div with javascript

Trying to refresh a div every 60 seconds, using the following javascript: 尝试使用以下javascript每60秒刷新一次div:

<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load').fadeOut('slow').load('index.html #load').fadeIn("slow");
}, 60000);
</script>  

Here is the corresponding div: 这是对应的div:

<div class = "well" id="load">
  <center><h3><i class="icon-bar-chart"></i> Mt.Gox</h3></center>
  <center><h3>&#579;1 = <?php echo $usd; ?> USD</h3></center>
<hr>
  <center><h3><i class="icon-bar-chart"></i> BTC-e</h3></center>
  <center><h3>&#579;1 = <?php echo $usde; ?> USD</h3></center>
</div>

The div is currently not refreshing at all. div当前完全不刷新。

Thanks for any help! 谢谢你的帮助!

Ok that's because you are using the script incorrectly., You need to use callbacks as below 好的,那是因为您使用的脚本不正确。您需要使用以下回调

    <script type="text/javascript">
    var auto_refresh = setInterval(
    function ()
    {
    $('#load').fadeOut('slow',$(this).load('index.html #load', 
       function(){
          $(this).fadeIn("slow");
       })
    )
    }, 60000);
    </script>  

Edit: I misunderstood the code a bit so ignore my original comment below! 编辑:我有点误解了代码,所以请忽略下面我的原始评论!


Try to remove the #load in the url. 尝试删除网址中的#load It's perfectly valid according to the jQuery API , but I think in this case it can cause reduncancy. 根据jQuery API ,它是完全有效的,但我认为在这种情况下,它可能导致冗余。

That is, make it like this instead: 也就是说,改为这样:

$('#load').fadeOut('slow').load('index.html').fadeIn("slow");

When you call .load() on an element it will load into that element, if you also specify the same element in the Url to load - I think it can cause a problem. 当您在某个元素上调用.load()时,如果您还在Url中指定要加载的相同元素,则该元素也会加载到该元素中-我认为这可能会引起问题。

Please see this link: Origin null is not allowed by Access-Control-Allow-Origin 请查看此链接: Access-Control-Allow-Origin不允许使用Origin null

Also that issue, I think that you shouldn't load html data via file:/// URL. 也是这个问题,我认为您不应该通过file:/// URL加载html数据。 Why don't you load a div in the same file. 为什么不将div加载到同一文件中。 Forexample: 例如:

$('#load').fadeOut('slow').load($('#test').css('display', 'inline')).fadeIn("slow"); $('#load')。fadeOut('slow')。load($('#test')。css('display','inline'))。fadeIn(“ slow”);

and the div tag is like this: div标签是这样的:

div id="test" style="display:none;" div id =“ test” style =“ display:none;”

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

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