溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Ajax獲得站點(diǎn)文件內(nèi)容的方法教程

發(fā)布時(shí)間:2021-09-30 15:57:10 來源:億速云 閱讀:128 作者:iii 欄目:web開發(fā)

本篇內(nèi)容介紹了“Ajax獲得站點(diǎn)文件內(nèi)容的方法教程”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

一個(gè)簡單的Ajax實(shí)例:選擇一部著作,會通過 Ajax 實(shí)時(shí)獲得相關(guān)的名字。

把4個(gè)html文件放到 web站點(diǎn) 的同一個(gè)文件下。

index.html

復(fù)制代碼 代碼如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>一個(gè)簡單的不涉及服務(wù)器的Ajax實(shí)例</title>
<script type="text/javascript">
// 聲明一個(gè)引用 XMLHttpRequest 的變量
var xhr = null;
// 選擇一個(gè)著作時(shí)調(diào)用的函數(shù)
function updateCharacters() {
var selectShow = document.getElementById("selShow").value;
if (selectShow == "") {
document.getElementById("divCharacters").innerHTML = "";
return ;
}
// 實(shí)例化一個(gè) 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>
我們的第一個(gè)Ajax實(shí)例
<br></br>
選擇一個(gè)名著:
<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>
返回,名著中主要任務(wù):
<br>
<div id="divCharacters">
<select>
</select>
</div>
</body>
</html>


xyj.html

復(fù)制代碼 代碼如下:

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


hlm.html

復(fù)制代碼 代碼如下:

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


shz.html

復(fù)制代碼 代碼如下:

<select>
<option>林沖</option>
<option>李逵</option>
<option>宋江</option>
<option>時(shí)遷</option>
</select>


sgyy.html

復(fù)制代碼 代碼如下:

<select>
<option>劉備</option>
<option>關(guān)羽</option>
<option>張飛</option>
<option>曹操</option>
<option>小喬</option>
<option>諸葛亮</option>
</select>

“Ajax獲得站點(diǎn)文件內(nèi)容的方法教程”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI