原生Ajax 和jQuery Ajax的區(qū)別示例分析
前言:這次介紹的是利用ajax與后臺進(jìn)行數(shù)據(jù)交換的小例子,所以demo必須通過服務(wù)器來打開。服務(wù)器環(huán)境非常好搭建,從網(wǎng)上下載wamp或xampp,一步步安裝就ok,然后再把寫好的頁面放在服務(wù)器中指定的位置。打開時(shí),在瀏覽器地址欄輸入“l(fā)ocalhost/指定頁面”或者“127.0.0.1/指定頁面”打開。
下面列出demo的HTML、PHP、原生ajax 、jq ajax代碼。
HTML代碼:
<!doctype html>
<html>
<head>
<title>ajax示例</title>
<meta charset='utf-8' />
<link rel="stylesheet" type="text/css" href="css/common.css" />
<style type="text/css">
.main{height:400px;width:800px;margin:100px auto 0;border:1px solid #000;}
.list{height:400px;width:200px;float:left;background:#ddd;}
.inf{height:400px;width:600px;float:right;background:#ccc;text-align:center;}
.list li{width:200px;text-align:center;margin:50px 0 0;font-size:24px;cursor: pointer;
}
.inf img{width:360px;height:270px;margin:15px auto;}
.inf p{width:580px;text-align:left;text-indent:2em;font-size:14px;margin:0 10px;}
</style>
</head>
<body>
<div class='main'>
<div class='list' id='list'>
<ul>
<li name='spring' id='spring'>春</li>
<li name='summer' id='summer'>夏</li>
<li name='fall' id='fall'>秋</li>
<li name='winter' id='winter'>冬</li>
</ul>
</div>
<div class='inf' id='inf'>
<!--要插入的內(nèi)容-->
</div>
</div>
</body>
<script type="text/javascript" charset="utf-8" src="js/jQuery.js"></script>
</html>
PHP代碼:
<?php
$details = array (
'spring' => "<img src='images/spring.jpg' alt='' /><p>人間四月芳菲盡,山寺桃花始盛開</p>",
'summer' => "<img src='images/summer.jpg' alt='' /><p>水晶簾動微風(fēng)起,滿架薔薇一院香</p>",
'fall' => "<img src='images/fall.jpg' alt='' /><p>金井梧桐秋葉黃,珠簾不卷夜來霜</p>",
'winter' => "<img src='images/winter.jpg' alt='' /><p>梅須遜雪三分白,雪卻輸梅一段香</p>"
);
echo $details[$_REQUEST['LiName']];
?>
原生ajax:
<script type="text/javascript">
var lis = document.getElementById('list').getElementsByTagName('li');
window.onload = initPage;
function initPage() {
for (var i=0; i<lis.length; i++) {
txt = lis[i];
txt.onclick = function () {
getDetails(this.id);
}
}
}
function creatRequest() {
try {
request = new XMLHttpRequest();
}
catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (otherMS) {
try {
request = new ActiveXObject("Miscrosoft.XMLHTTP");
}
catch (failed) {
request = null;
}
}
}
return request;
}
function getDetails(itemName) {
request = creatRequest();
if (request == null) {
alert('沒有成功創(chuàng)建請求')
return;
}
var url = "getDetails.php?LiName="+escape(itemName);
request.open("GET",url,true);
request.onreadystatechange = displayDetails;
request.send(null);
}
function displayDetails() {
if (request.readyState == 4) {
if (request.status == 200) {
detailDiv = document.getElementById("inf");
detailDiv.innerHTML = request.responseText;
}
}
}
</script>
JQ ajax:
<script type="text/javascript">
$('#list li').click ( function () {
$.ajax({
type:'GET',
data:'',
url:"getDetails.php?LiName="+this.id,
success:function(data){
$('#inf').html(data);
},
dataType:'text',
error:function (){
alert("失敗!");
}
})
});
</script>
相關(guān)文章
jquery的ajax從純真網(wǎng)(cz88.net)獲取IP地址對應(yīng)地區(qū)名
使用jquery的ajax,輕松從純真網(wǎng)(cz88.net)獲取IP地址對應(yīng)地區(qū)名2009-12-12
iframe里面的元素觸發(fā)父窗口元素事件的jquery代碼
這篇文章主要介紹了jquery:iframe里面的元素怎樣觸發(fā)父窗口元素的事件,很簡單,但很實(shí)用,需要的朋友可以看看2014-10-10
jQuery實(shí)現(xiàn)的仿百度,仿谷歌搜索下拉框效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的仿百度,仿谷歌搜索下拉框效果,結(jié)合實(shí)例形式分析了基于jQuery的鼠標(biāo)事件響應(yīng)與頁面元素動態(tài)操作相關(guān)技巧,需要的朋友可以參考下2016-12-12
JQuery判斷checkbox是否選中及其它復(fù)選框操作方法合集
這篇文章主要介紹了JQuery判斷checkbox是否選中及其它復(fù)選框操作方法合集,本文匯總了網(wǎng)上解決這個問題比較好的幾篇文章,需要的朋友可以參考下2015-06-06
jQuery ''行 4954 錯誤: 不支持該屬性或方法'' 的問題解決方法
這個問題只在IE下出現(xiàn)。詭異的是,對于出現(xiàn)這個問題的頁面,重新刷新一下就又好了,Ajax 工作一切正常。順便說一下,我的 jQuery 版本是 1.4.2。2011-01-01

