我遇到了挑战:
如何使用AJAX在页面上重新加载<div>
,而不加载条形图并且在加载内容时不会消失。
只要加载新内容,我想保留以前生成的内容。
这是我的脚本,但在循环中加载内容时会闪烁:
<script type="text/javascript">
function showCargo_34(urlparms)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttpshowCargo_34=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttpshowCargo_34=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttpshowCargo_34.onreadystatechange=function()
{
if (xmlhttpshowCargo_34.readyState==4 && xmlhttpshowCargo_34.status==200)
{
document.getElementById("Cargo_34").innerHTML=xmlhttpshowCargo_34.responseText;
}
}
xmlhttpshowCargo_34.open("GET","modules/desktop/desktop/script_cargo.php?"+urlparms,true); // modules/catalogitem/script_categorylist.php?"+urlparms
xmlhttpshowCargo_34.send();
};
</script>
<div id="Cargo_34"></div>
<script>
function repeat_Cargo_34() {
showCargo_34('baseid=1&moduleid=10&gridmoduleid=133&speed=180&x=3&y=4');
setTimeout( repeat_Cargo_34, 20000);
};
repeat_Cargo_34();
</script>
答案 0 :(得分:1)
我会将jquery用于ajax。它更快更简单。
简单方法
$("#element_id").load("http://url.com");
更高级的方式,您可以找到here
你也在谈论保留previoius内容。好吧,只需使用+ = for innerHTML:
document.getElementById("Cargo_34").innerHTML += xmlhttpshowCargo_34.responseText;
答案 1 :(得分:1)
使用jQuery的AJAX,它更容易。查看load()函数,例如setInterval。 e.g:
setInterval(function(){ $('#div').load('file.html'); }, 5000}
每隔5秒就会将file.html加载到<div id="id">
答案 2 :(得分:1)
如果您不想使用jquery,我建议制作第二个ajax函数,并使当前函数更改为一个不可见的div,当加载和更改完成时,您将回显以下行。
//Echo this once all data has been loaded and changed.
//$newurlPalms is what you would have echoed normally but in a urlpalm.
echo "<style onload='showCargo2($newurlpalms)'></style>";
//so make this only load and save all the data and then echo above line in
//a hidden div
function showcargo(urlpalms){ajaxcode}
//make this show the data in the right div.
function showcargo2(urlpalms){ajaxdoce}
像这样它不会删除数据,一旦加载完成,回声将导致第二个函数自动运行,而不是能够立即显示数据。
答案 3 :(得分:0)
这种情况不存在。
<强>解释强>
我的问题中的标准html请求调用在加载时不会按innerHTML
放置内容。这意味着在加载内容时,它将按原样显示,直到内容以JSON完成并且ajax调用函数完成。
错误出现在代码的另一部分。