Javascript 如何刷新 DIV 内容?

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

How do I refresh a DIV content?

javascriptphpjqueryhtml

提问by cyberion1985

<div id='here'></div>

I am trying to refresh a certain div within a bunch of divs. The div content is basically generated by PHP along with data from a MySQL database in addition to many variables being sent with the help of an XMLHttpRequest.

我正在尝试刷新一堆 div 中的某个 div。除了在 XMLHttpRequest 的帮助下发送的许多变量之外,div 内容基本上是由 PHP 以及来自 MySQL 数据库的数据生成的。

The idea is to reload/refresh the div itself and not load a php file or to overwrite it with .textor .html. In this case, I cannot use .load('file.php')

这个想法是重新加载/刷新 div 本身,而不是加载 php 文件或用.text或覆盖它.html。在这种情况下,我不能使用.load('file.php')

What I have tried :

我尝试过的:

<script type='text/javascript'>
    function updateDiv()
    { 
        document.getElementById("here").innerHTML = document.getElementById("here").innerHTML ;
    } 
</script>

and (for a every 3 second refresh) :

和(每 3 秒刷新一次):

$(document).ready(function () {
    setInterval(function () {
        $('#here').load('#here'));
    }, 3000);
});

Ideally, I would require something like :

理想情况下,我需要类似的东西:

function reloadDIV () {document.getElementById("here").innerHTML.reload}

function reloadDIV () {$('#here').load(self)} 

so that I can use it in a onClick :

这样我就可以在 onClick 中使用它:

<a onclick='reloadDIV ();'>reload div</a>

回答by Steve

To reload a section of the page, you could use jquerys loadwith the current url and specify the fragment you need, which would be the same element that loadis called on, in this case #here:

要重新加载页面的一部分,您可以使用load带有当前 url 的jquerys并指定您需要的片段,这将是load被调用的相同元素,在这种情况下#here

function updateDiv()
{ 
    $( "#here" ).load(window.location.href + " #here" );
}

This function can be called within an interval, or attached to a click event

这个函数可以在一个时间间隔内调用,或者附加到一个点击事件

回答by Олег Шилюк

For div refreshing without creating div inside yours with same id, you should use this inside your function

对于 div 刷新而不在你的内部创建具有相同 id 的 div,你应该在你的函数中使用它

$("#yourDiv").load(" #yourDiv > *");

回答by pc_

Complete working code would look like this:

完整的工作代码如下所示:

<script> 
$(document).ready(function(){
setInterval(function(){
      $("#here").load(window.location.href + " #here" );
}, 3000);
});
</script>

<div id="here">dynamic content ?</div>

self reloading div container refreshing every 3 sec.

自重载 div 容器每 3 秒刷新一次。

回答by Neha Jain

You can use jQuery to achieve this using simple $.getmethod. .htmlwork like innerHtml and replace the content of your div.

您可以使用 jQuery 使用简单的$.get方法来实现这一点。.html像innerHtml一样工作并替换div的内容。

$.get("/YourUrl", {},
      function (returnedHtml) {
      $("#here").html(returnedHtml);
});

And call this using javascript setIntervalmethod.

并使用 javascriptsetInterval方法调用它。