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
Auto Refresh a div without loading from another page
提问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 DIV
automatically. So there are a number of ways you can force an element
to re-render (without a reload) - the most effective one I found was to quickly switch the display
style of the element
in question.
如果我理解正确,您的问题是DIV
自动重新渲染。因此,您可以通过多种方式强制element
重新渲染(无需重新加载)——我发现最有效的方法是快速切换相关display
样式element
。
That is:
那是:
Set the display
property 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.
活生生的例子在这里。