javascript 自动刷新div而不从另一个页面加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17419061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:15:07  来源:igfitidea点击:

Auto Refresh a div without loading from another page

javascriptjquery

提问by Astro

example

例子

 <script type="text/javascript"       src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function () {
    $('#div').load('another.page');
}, 1000);
    </script>

i dont want 'another.page' to load i just want to refreh the the '#div' div is it possible if it is how

我不想加载 'another.page' 我只想刷新 '#div' div 是否有可能,如果它是如何

thank you

谢谢

回答by Karlen Kishmiryan

If I understand you correctly, your problem is to re-render the DIVautomatically. So there are a number of ways you can force an elementto re-render (without a reload) - the most effective one I found was to quickly switch the displaystyle of the elementin question.

如果我理解正确,您的问题是DIV自动重新渲染。因此,您可以通过多种方式强制element重新渲染(无需重新加载)——我发现最有效的方法是快速切换相关display样式element

That is:

那是:

Set the displayproperty to none:

display属性设置为none

element.style.display = 'none';

and then return it to block

然后将其返回到 block

element.style.display = 'block';

Here is the working demo of your example in JSFiddle.

这是您在JSFiddle中的示例的工作演示。

Note:It's pure JavaScript.

注意:它是纯的JavaScript

回答by Sergio

Here is an example of how you can update the content of a div without reloading the page. It's a bit unclear what you want from your question so I hope this helps.

下面是一个示例,说明如何在不重新加载页面的情况下更新 div 的内容。有点不清楚你想从你的问题中得到什么,所以我希望这会有所帮助。

html

html

<div id="divID"></div>

javascript

javascript

var counter = 1;
var auto_refresh = setInterval(
function () {
    var newcontent= 'Refresh nr:'+counter;
    $('#divID').html(newcontent);
    counter++;
}, 1000);

Live example here.

活生生的例子在这里

回答by xpy

You can't load just a part of the page just like that. But you can load all the page and fetch just the part you want. eg:

你不能像那样只加载页面的一部分。但是您可以加载所有页面并只获取您想要的部分。例如:

$.get('anoter_page.html',function(data){
myDiv = $(data).find('#divID');
$('#divID').html(myDiv.html());
})

Example: JSFiddle

示例:JSFiddle