Ajax獲得站點文件內(nèi)容實例不涉及服務器
更新時間:2014年05月15日 10:22:59 作者:
選擇一部著作,會通過 Ajax 實時獲得相關的名字,一個簡單的不涉及服務器的Ajax實例,需要的朋友可以參考下
一個簡單的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;
// 選擇一個著作時調(diào)用的函數(shù)
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>
把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;
// 選擇一個著作時調(diào)用的函數(shù)
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>
您可能感興趣的文章:
- JQuery中Ajax的Post提交在IE下中文亂碼的解決方法
- 告別AJAX實現(xiàn)無刷新提交表單
- ajax跨域請求js拒絕訪問的解決方法
- Ajax動態(tài)加載數(shù)據(jù)庫示例
- Ajax 無刷新在注冊用戶名時判斷是否為空是否被使用
- 用AJAX技術實現(xiàn)在自己Blog上聚合并顯示朋友Blog的最新文章
- iframe式ajax調(diào)用示例代碼
- ThinkPHP使用心得分享-ThinkPHP + Ajax 實現(xiàn)2級聯(lián)動下拉菜單
- Ajax 給 XMLHttpReq.onreadystatechange傳遞參數(shù)
- $.ajax傳JSON數(shù)據(jù)到后臺的注意事項小結
- Ajax核心技術代碼分享
相關文章
ajax中指定innerHTML時如何應用其中的SCRIPT的研究
ajax中指定innerHTML時如何應用其中的SCRIPT的研究...2007-03-03
AJAX入門之深入理解JavaScript中的函數(shù)
AJAX入門之深入理解JavaScript中的函數(shù)...2006-06-06
使用Ajax方法實現(xiàn)Form表單的提交及注意事項
這篇文章主要介紹了使用Ajax方法實現(xiàn)Form表單的提交及注意事項,需要的朋友可以參考下2017-07-07

