2016-07-06 52 views
0

我的代码是这样的我从教程网站拿它不记得在哪里请告诉我在哪里把加载图标放在这个巨大的垃圾代码我不明白。 请通过jsfiddle或其他任何地方向我展示一个例子。 如何把这个ajax形式的加载图标,请任何人告诉我?

<script language = "javascript" type = "text/javascript"> 
    <!-- 
     //Browser Support Code 
     function ajaxFunction(){ 
      var ajaxRequest; // The variable that makes Ajax possible! 

      try { 
       // Opera 8.0+, Firefox, Safari 
       ajaxRequest = new XMLHttpRequest(); 
      }catch (e) { 
       // Internet Explorer Browsers 
       try { 
       ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
       }catch (e) { 
       try{ 
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
       }catch (e){ 
        // Something went wrong 
        alert("Your browser broke!"); 
        return false; 
       } 
       } 
      } 

      // Create a function that will receive data 
      // sent from the server and will update 
      // div section in the same page. 

      ajaxRequest.onreadystatechange = function(){ 
       if(ajaxRequest.readyState == 4){ 
       var ajaxDisplay = document.getElementById('ajaxDiv'); 
       ajaxDisplay.innerHTML = ajaxRequest.responseText; 
       } 
      } 

      // Now get the value from user and pass it to 
      // server script. 

      var age = document.getElementById('age').value; 
      var wpm = document.getElementById('wpm').value; 
      var sex = document.getElementById('sex').value; 
      var queryString = "?age=" + age ; 

      queryString += "&wpm=" + wpm + "&sex=" + sex; 
      ajaxRequest.open("GET", "ajax-example.php" + queryString, true); 
      ajaxRequest.send(null); 
     } 
    //--> 
    </script> 

    <form name = 'myForm'> 
    Max Age: <input type = 'text' id = 'age' /> <br /> 
    Max WPM: <input type = 'text' id = 'wpm' /> 
    <br /> 

    Sex: <select id = 'sex'> 
     <option value = "m">m</option> 
     <option value = "f">f</option> 
    </select> 

    <input type = 'button' onclick = 'ajaxFunction()' value = 'Query MySQL'/> 

    </form> 

    <div id = 'ajaxDiv'>Your result will display here</div> 

回答

1

首先,检查this出来的就绪状态的详细信息。

我会在ajaxRequest.open("GET", "ajax-example.php" + queryString, true);之前加载一个,并在if (xmlhttp.readyState==4上删除它。

所以添加HTML您希望加载图标出现: <span id="loading"></span>

2.

然后之前ajaxRequest.open...插入加载图片:

document.getElementById("loading").innerHTML = '<img src="loading.gif" />';

而且里面放if (xmlhttp.readyState == 4

document.getElementById("loading").innerHTML = '';

相关问题