简体   繁体   English

jQuery自动刷新div搞砸了

[英]jQuery Auto refresh div messing up

I have a script that auto-refreshes a certain div on the page (That I got from another post on here) 我有一个脚本会自动刷新页面上的某个div(这是我从此处的另一篇文章中获得的)

<script type="text/javascript">
    var auto_refresh = setInterval(
    function(){
        $('#refresh').load('index.php?_=' +Math.random()).fadeIn("slow");
    }, 10000); // refresh every 10000 milliseconds
</script>
...............
<div id="refresh">
  <!-- Some PHP Code -->
</div>

This refreshes, however when it does, I takes the entire html document and puts it into the div. 这会刷新,但是当它执行时,我将整个html文档放入div中。 Like this: 像这样:

div中的主体

As you can see, the refreshed div (the one marked in red) is getting the body shouved into it. 如您所见,刷新后的div(标记为红色的那个)正将主体塞入其中。 Any ideas??? 有任何想法吗???

First off, you are loading the entire page into the divider, thus causing the file to reload entirely. 首先,将整个页面加载到分隔符中,从而导致文件完全重新加载。 Instead, you should be having the Recent Posts divider load from a single file, even on the first page load. 相反,您应该从单个文件中加载“最近发布”分隔符,即使在首页加载时也是如此。 Then have that consistently refresh over time. 然后随着时间的推移不断刷新。

Secondly, you should be transferring as little data as possible from your server to your clients. 其次,应该将尽可能少的数据从服务器传输到客户端。 At most, you should use a minimalistic checksum of sorts (number of messages, for instance) to confirm that the client and server are synced up. 至多,您应该使用一种简单的校验和(例如,消息数)来确认客户端和服务器已同步。

Lastly, if you choose to use this format, aim to transfer your data in something such as JSON or XML and have the client display it on the page. 最后,如果您选择使用这种格式,则希望以JSONXML之类的格式传输数据,并让客户端在页面上显示它。 Transferring the styled HTML increases network overhead and is not the best practice. 传输样式化的HTML会增加网络开销,而不是最佳实践。

You are loading entire page to the div. 您正在将整个页面加载到div。 Modify the code to use only part of the document that is fetched: 修改代码以仅使用获取的文档的一部分:

    <script type="text/javascript">
        var auto_refresh = setInterval(
        function(){
            $('#refresh').empty();
            $('#refresh').load('index.php?_=' +Math.random()+' #refresh').fadeIn("slow");
        }, 10000); // refresh every 10000 milliseconds
    </script>

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

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