在ajax获取文件内容时显示加载图像

Aya*_*lik 4 ajax

我是ajax的新手,阅读了一些tut内容,制作了具有多个按钮的lil脚本,并在每次单击时将在特定的div中加载一个php文件。为此我使用这个

function loadXMLDoc()
{
var xmlhttp;
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("1").innerHTML=xmlhttp.responseText;
        }
  }
xmlhttp.open("GET","feedchck.php",true);
xmlhttp.send();
}  
Run Code Online (Sandbox Code Playgroud)

动作按钮:

<button type="button" onclick="loadXMLDoc();">Request</button>
<div id="1">asdf</div>
Run Code Online (Sandbox Code Playgroud)

我到了这一部分,然后单击php文件即可完美加载。但是因为需要花费一些时间来处理,因此显示空白区域。那么是否可以在处理过程中显示正在加载的图像或文本,完成后将隐藏图像并显示文件内容?
帮助表示赞赏:}
欢呼

sat*_*ish 6

<button type="button" onclick="loadXMLDoc();">Request</button>
<!--An empty tag - this be the loading image-->
<span id="loading"></span>
<div id="1">asdf</div>

<script>
function loadXMLDoc() {
    var xmlhttp;
    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("loading").innerHTML = ''; // Hide the image after the response from the server
            document.getElementById("1").innerHTML = xmlhttp.responseText;
        }
    }
    document.getElementById("loading").innerHTML = '<img src="../loading.gif" />'; // Set here the image before sending request
    xmlhttp.open("GET", "feedchck.php", true);
    xmlhttp.send();
}
</script>
Run Code Online (Sandbox Code Playgroud)