Javascript 自动重新加载 div 容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2595005/
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-08-23 01:04:06  来源:igfitidea点击:

automatic reload of div container

javascriptjquerycsshtml

提问by input

instead of a whole page refresh after a certain time, i'd just like a specific div container to reload/refresh. is there any way to do this?

而不是在一段时间后刷新整个页面,我只是想要一个特定的 div 容器来重新加载/刷新。有没有办法做到这一点?

<div id="wrapper">
<div id="quoteContainer"></div>
</div>

回答by Nicholas Murray

You can get the effect you desire with jQuery and Googles ajax api

您可以使用 jQuery 和 Googles ajax api 获得您想要的效果

<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 ()
{
   $('#load_latest_scores').load('latest_scores.html');
}, 10000); // refresh every 10000 milliseconds
</script>
<body>
<div id="load_latest_scores"> </div>
</body>

回答by Nick Craver

If you had a page that served quotes, like quote.htmlfor example, you could do this:

如果您有一个提供引号的页面,quote.html例如,您可以这样做:

setInterval(refreshQuote, 10000); //every 10 seconds

function refreshQuote() {
  $("#quoteContainer").load("quote.html");
}

In this case the expected return from quote.html(or whatever source you have) it a simple string that is the quote, it will take this and replace the content of <div id="quoteContainer"></div>with it.

在这种情况下,预期返回quote.html(或您拥有的任何来源)它是一个简单的字符串,即引用,它将接受它并<div id="quoteContainer"></div>用它替换内容。

回答by Sebastian Marcet

i think that one aproach would be

我认为一种方法是

<script>
function render (){
$('#mydiv').html("<b>new stuff</b>")
}
window.setInterval(render, 500);
</script>