最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery的實(shí)現(xiàn)原理的模擬代碼 -5 Ajax

 更新時(shí)間:2010年08月07日 15:35:29   作者:  
對(duì)于 xhr 對(duì)象來(lái)說(shuō),我們主要通過(guò)異步方式訪問(wèn)服務(wù)器,在 onreadystatechange 事件中處理服務(wù)器回應(yīng)的內(nèi)容。簡(jiǎn)單的 xhr 使用如下所示。
復(fù)制代碼 代碼如下:

// 創(chuàng)建 XHR 對(duì)象
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
throw new Error("Ajax is not supported by this browser");
}
function ready()
{
alert("Start......");
// 通過(guò)事件來(lái)處理異步請(qǐng)求
xhr.onreadystatechange = function()
{
if( xhr.readyState == 4 )
{
alert( "Ready.");
if( xhr.status == 200 )
{
alert("成功獲得服務(wù)器返回的結(jié)果.");
// 請(qǐng)求結(jié)束之后,可以獲取服務(wù)器返回的內(nèi)容
alert( xhr.responseText );
// 獲取服務(wù)器返回的 json 對(duì)象
var alice = eval( "(" + xhr.responseText + ")" );
alert( alice.name );
}
}
};
// 設(shè)置請(qǐng)求參數(shù)
xhr.open("get", "data.json" );
xhr.send( null );
}

jQuery 簡(jiǎn)單地包裝了對(duì) xhr 對(duì)象的使用,通過(guò)對(duì) jQuery 對(duì)象增加常用的訪問(wèn)方法,然后,提供給 jQuery 對(duì)象來(lái)使用。
復(fù)制代碼 代碼如下:

// 主要的擴(kuò)展在 jQuery.ajax 中。
jQuery.extend({ // #6299
// 請(qǐng)求的默認(rèn)參數(shù)
ajaxSettings: {
url: location.href,
type: "GET",
contentType: "application/x-www-form-urlencoded",
data: null,
xhr: window.XMLHttpRequest && (window.location.protocol !== "file:" || !window.ActiveXObject) ?
function () {
return new window.XMLHttpRequest();
} :
function () {
try {
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { }
}
},
// 用來(lái)設(shè)置 jQuery.ajaxSettings ,設(shè)置請(qǐng)求的參數(shù)
ajaxSetup: function (settings) {
jQuery.extend(jQuery.ajaxSettings, settings);
},
ajax: function (origSettings) { // 實(shí)際的 ajax 函數(shù)
var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings);
// 創(chuàng)建 xhr 對(duì)象
xhr = s.xhr();
// 回調(diào)函數(shù)
var onreadystatechange = xhr.onreadystatechange = function (isTimeout) {
if (xhr.readyState === 4) {
if (xhr.status == 200) {
s.success.call(origSettings, xhr.responseText);
}
}
};
// 設(shè)置請(qǐng)求參數(shù)
xhr.open(s.type, s.url);
// Send the data 發(fā)出請(qǐng)求
xhr.send(s.data);
// return XMLHttpRequest to allow aborting the request etc.
return xhr;
},
// 使用 get 方式發(fā)出 ajax 請(qǐng)求的方法
get: function (url, data, callback, type) {
// shift arguments if data argument was omited
if (jQuery.isFunction(data)) {
type = type || callback;
callback = data;
data = null;
}
return jQuery.ajax({
type: "GET",
url: url,
data: data,
success: callback,
dataType: type
});
}

}); // #6922
// 擴(kuò)展 jQuery 對(duì)象,增加 load 方法
jQuery.fn.extend(
{
load: function (url) {
var self = this;
jQuery.get(url, function (data) {
self.each(function () {
this.innerHTML = data;
}
)
}
)
}
}
)

在頁(yè)面中,可以如下使用。
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<input type="button" id="btn" value="Click me" />
<div id="msg">
</div>
<script src="jQuery-core.js" type="text/javascript"></script>
<script src="jQuery-event-2.js" type="text/javascript"></script>
<script src="jQuery-data.js" type="text/javascript"></script>
<script src="jQuery-extend.js" type="text/javascript"></script>
<script src="jQuery-ajax.js" type="text/javascript"></script>
<script type="text/javascript">
$("#btn").click(function () {
$("#msg").load("hello.txt");
})

</script>
</body>
</html>

相關(guān)文章

  • 使用struts2+Ajax+jquery驗(yàn)證用戶(hù)名是否已被注冊(cè)

    使用struts2+Ajax+jquery驗(yàn)證用戶(hù)名是否已被注冊(cè)

    這篇文章主要介紹了使用struts2+Ajax+jquery驗(yàn)證用戶(hù)名是否已被注冊(cè)的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • jQuery動(dòng)態(tài)創(chuàng)建html元素的常用方法匯總

    jQuery動(dòng)態(tài)創(chuàng)建html元素的常用方法匯總

    這篇文章主要介紹了jQuery動(dòng)態(tài)創(chuàng)建html元素的常用方法,包括jQuery追加元素、遍歷數(shù)組以及使用模板等方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-09-09
  • 使用jQuery實(shí)現(xiàn)購(gòu)物車(chē)

    使用jQuery實(shí)現(xiàn)購(gòu)物車(chē)

    這篇文章主要為大家詳細(xì)介紹了使用jQuery實(shí)現(xiàn)購(gòu)物車(chē),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • jQuery插件 tabBox實(shí)現(xiàn)代碼

    jQuery插件 tabBox實(shí)現(xiàn)代碼

    最近對(duì)js以及jq產(chǎn)生了濃厚的興趣,看到j(luò)q有很多很好用的插件,功能各異,包括webUI,jqGrid等等。心里萌發(fā)了制作屬于自己的jq插件的想法。
    2010-02-02
  • jQuery 行背景顏色的交替顯示(隔行變色)實(shí)現(xiàn)代碼

    jQuery 行背景顏色的交替顯示(隔行變色)實(shí)現(xiàn)代碼

    主要是利用了jquery的attr為行添加樣式來(lái)實(shí)現(xiàn)的,具體的代碼如下。
    2009-12-12
  • html文件中jquery與velocity變量中的$沖突的解決方法

    html文件中jquery與velocity變量中的$沖突的解決方法

    在使用velocity模版引擎的環(huán)境下,使用jquery時(shí)其中$與velocity變量中的$沖突,下面有個(gè)不錯(cuò)的解決方法,感興趣的朋友可以參考下
    2013-11-11
  • 最新評(píng)論

    南安市| 拉孜县| 甘谷县| 桂东县| 大同市| 青田县| 岳池县| 顺昌县| 邛崃市| 灵山县| 双流县| 景谷| 晋州市| 文化| 昌宁县| 郎溪县| 静乐县| 始兴县| 和田市| 庆城县| 阳春市| 河源市| 靖江市| 寿阳县| 北流市| 峡江县| 沁阳市| 张家川| 宿迁市| 黄山市| 五莲县| 乐山市| 綦江县| 揭阳市| 东台市| 淮北市| 高州市| 麦盖提县| 垫江县| 遂宁市| 阜平县|