Ajax jQuery淡入php内容
我使用计时器调用ajax函数,该函数从单独的php页面加载响应。我想让它的内容淡出,然后淡入,与回应总是如此频繁 这是我的代码——我可以抓取内容,让它改变,只是在我不知所措的时候逐渐消失Ajax jQuery淡入php内容,php,jquery,ajax,timer,fade,Php,Jquery,Ajax,Timer,Fade,我使用计时器调用ajax函数,该函数从单独的php页面加载响应。我想让它的内容淡出,然后淡入,与回应总是如此频繁 这是我的代码——我可以抓取内容,让它改变,只是在我不知所措的时候逐渐消失 <script type="text/javascript"> window.setTimeout('runMoreCode()',100); $(document).ready(function(){ $('#refreshRecords').fadeIn(2000); }); fu
<script type="text/javascript">
window.setTimeout('runMoreCode()',100);
$(document).ready(function(){
$('#refreshRecords').fadeIn(2000);
});
function runMoreCode()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("refreshRecords").innerHTML=xmlhttp.responseText;
$('#refreshRecords').fadeIn('slow');
}
}
xmlhttp.open("GET","NewRecords.php",true);
xmlhttp.send();
setTimeout("runMoreCode()",6000);
}
</script>
setTimeout('runMoreCode()',100);
$(文档).ready(函数(){
$(“#刷新记录”)。fadeIn(2000年);
});
函数runMoreCode()
{
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“refreshRecords”).innerHTML=xmlhttp.responseText;
$('refreshRecords').fadeIn('slow');
}
}
open(“GET”,“NewRecords.php”,true);
xmlhttp.send();
setTimeout(“runMoreCode()”,6000);
}
我试过用法登做,但运气不好。我还链接了最新的jQuery文件
谢谢
GCooper尝试使用jQuery的方法。这样,您就可以在得到响应(打开的PHP页面)时应用任何jQuery效果。应该使您的工作更容易,代码更可读。如果您想继续使用JS,并且不浪费资源使用jQuery,那么使用旧方法是可以的,但是既然您使用的是jQuery,为什么不一直使用它呢
还有一件事:可能看起来很愚蠢,但您确实有一个id为“refreshRecords”的div,并且显示设置为“none”,是吗?您需要这样做:
$.get({
URL: "NewRecords.php",
function(data){
//add fade in effects
$('#result').hide().HTML(data).fadeIn('slow');
}
});
// fade out old content
$('#refreshRecords').fadeOut('slow', function (){
// now that it is gone, swap content
$('#refreshRecords').html(xmlhttp.responseText);
// now fade it back in
$('#refreshRecords').fadeIn('slow');
});
我同意Shomz,您也应该使用JQuery的$.ajax()
函数