Ajax獲得網站檔案內容執行個體不涉及伺服器_AJAX相關

來源:互聯網
上載者:User
一個簡單的Ajax執行個體:選擇一部著作,會通過 Ajax 即時獲得相關的名字。

把4個html檔案放到 web網站 的同一個檔案下。

index.html
複製代碼 代碼如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>一個簡單的不涉及伺服器的Ajax執行個體</title>
<script type="text/javascript">
// 聲明一個引用 XMLHttpRequest 的變數
var xhr = null;
// 選擇一個著作時調用的函數
function updateCharacters() {
var selectShow = document.getElementById("selShow").value;
if (selectShow == "") {
document.getElementById("divCharacters").innerHTML = "";
return ;
}
// 執行個體化一個 XMLHttpRequest 對象
if (window.XMLHttpRequest) {
// 非IE
xhr = new XMLHttpRequest();
} else {
// IE
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = callbackHandler;
url = selectShow + ".html";
xhr.open("post", url, true);
xhr.send(null);
}
// this is the function that will repeatedly be called by our
// XMLHttpRequest object during the life cycle of request
function callbackHandler() {
if (xhr.readyState == 4) {
document.getElementById("divCharacters").innerHTML = xhr.responseText;
}
}
</script>
</head>
<body>
我們的第一個Ajax執行個體
<br></br>
選擇一個名著:
<br>
<select onchange="updateCharacters();" id="selShow">
<option value=""></option>
<option value="xyj">西遊記</option>
<option value="hlm">紅樓夢</option>
<option value="shz">水滸傳</option>
<option value="sgyy">三國演義</option>
</select>
<br></br>
返回,名著中主要任務:
<br>
<div id="divCharacters">
<select>
</select>
</div>
</body>
</html>

xyj.html
複製代碼 代碼如下:

<select>
<option>唐僧</option>
<option>孫悟空</option>
<option>豬八戒</option>
<option>唐僧</option>
<option>觀音姐姐</option>
<option>西天如來</option>
</select>

hlm.html
複製代碼 代碼如下:

<select>
<option>賈寶玉</option>
<option>林黛玉</option>
<option>薛寶釵</option>
</select>

shz.html
複製代碼 代碼如下:

<select>
<option>林沖</option>
<option>李逵</option>
<option>宋江</option>
<option>時遷</option>
</select>

sgyy.html
複製代碼 代碼如下:

<select>
<option>劉備</option>
<option>關羽</option>
<option>張飛</option>
<option>曹操</option>
<option>小喬</option>
<option>諸葛亮</option>
</select>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.