国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP開發(fā)基礎教程之onreadystatechange事件

一、onreadystatechange事件

當請求被發(fā)送到服務器時,我們需要執(zhí)行一些基于響應的任務。

每當 readyState 改變時,就會觸發(fā) onreadystatechange 事件。

readyState 屬性存有 XMLHttpRequest 的狀態(tài)信息。

下面是 XMLHttpRequest 對象的三個重要的屬性:


67.png


在onreadystatechange 事件中,我們規(guī)定當服務器響應已做好被處理的準備時所執(zhí)行的任務。

當 readyState 等于 4 且狀態(tài)為 200 時,表示響應已就緒

注:onreadystatechange 事件被觸發(fā) 5 次(0 - 4),對應著 readyState 的每個變化。

二、使用Callback函數(shù)

callback 函數(shù)是一種以參數(shù)形式傳遞給另一個函數(shù)的函數(shù)。

如果您的網(wǎng)站上存在多個 AJAX 任務,那么您應該為創(chuàng)建 XMLHttpRequest 對象編寫一個標準的函數(shù),并為每個 AJAX 任務調(diào)用該函數(shù)。

該函數(shù)調(diào)用應該包含 URL 以及發(fā)生 onreadystatechange 事件時執(zhí)行的任務(每次調(diào)用可能不盡相同):

下面演示一個頁面有兩個AJAX任務的情況:

代碼5_1.php 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript">
var xmlhttp;
//標準函數(shù)
function loadXMLDoc(url,cfunc)
{
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=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function myFunction1()
{
loadXMLDoc("5_2.txt",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv1").innerHTML=xmlhttp.responseText;
    }
  });
}
function myFunction2()
{
loadXMLDoc("5_3.txt",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv2").innerHTML=xmlhttp.responseText;
    }
  });
}
</script>
</head>
<body>
<!-- 按下按鈕,調(diào)用myFunction1() -->
<div id="myDiv1"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="myFunction1()">NO:1 通過 AJAX 改變內(nèi)容</button>
<hr/>
<!-- 按下按鈕,調(diào)用myFunction2() -->
<div id="myDiv2"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="myFunction2()">NO:2通過 AJAX 改變內(nèi)容</button>
</body>
</html>

代碼5_2.txt  

AJAX is not a programming language.

It is just a technique for creating better and more interactive web applications.

代碼5_3.txt

AJAX 不是新的編程語言,而是一種使用現(xiàn)有標準的新方法。


 


繼續(xù)學習
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript"> var xmlhttp; //標準函數(shù) function loadXMLDoc(url,cfunc) { 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=cfunc; xmlhttp.open("GET",url,true); xmlhttp.send(); } function myFunction1() { loadXMLDoc("5_2.txt",function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv1").innerHTML=xmlhttp.responseText; } }); } function myFunction2() { loadXMLDoc("5_3.txt",function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv2").innerHTML=xmlhttp.responseText; } }); } </script> </head> <body> <!-- 按下按鈕,調(diào)用myFunction1() --> <div id="myDiv1"><h2>Let AJAX change this text</h2></div> <button type="button" onclick="myFunction1()">NO:1 通過 AJAX 改變內(nèi)容</button> <hr/> <!-- 按下按鈕,調(diào)用myFunction2() --> <div id="myDiv2"><h2>Let AJAX change this text</h2></div> <button type="button" onclick="myFunction2()">NO:2通過 AJAX 改變內(nèi)容</button> </body> </html>
提交重置代碼
章節(jié)
筆記
提問
課件
反饋
捐贈

新版php入門教程