国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

jQuery中文參考手冊(cè) / jQuery load() 方法

jQuery load() 方法

jQuery - AJAX load() 方法


jQuery load() 方法

jQuery load() 方法是簡(jiǎn)單但強(qiáng)大的 AJAX 方法。

load() 方法從服務(wù)器加載數(shù)據(jù),并把返回的數(shù)據(jù)放入被選元素中。

語(yǔ)法:

$(selector).load(URL,data,callback);

必需的 URL 參數(shù)規(guī)定您希望加載的 URL。

可選的 data 參數(shù)規(guī)定與請(qǐng)求一同發(fā)送的查詢(xún)字符串鍵/值對(duì)集合。

可選的 callback 參數(shù)是 load() 方法完成后所執(zhí)行的函數(shù)名稱(chēng)。

這是示例文件("demo_test.txt")的內(nèi)容:

<h2>jQuery AJAX 是個(gè)非常棒的功能!</h2>

<p id="p1">這是段落的一些文本。</p>

下面的例子會(huì)把文件 "demo_test.txt" 的內(nèi)容加載到指定的 <div> 元素中:

實(shí)例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥(niǎo)教程(runoob.com)</title>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
	$("button").click(function(){
		$("#div1").load("/try/ajax/demo_test.txt");
	});
});
</script>
</head>
<body>

<div id="div1"><h2>使用 jQuery AJAX 修改文本內(nèi)容</h2></div>
<button>獲取外部?jī)?nèi)容</button>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

也可以把 jQuery 選擇器添加到 URL 參數(shù)。

下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的內(nèi)容,加載到指定的 <div> 元素中:

實(shí)例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("/try/ajax/demo_test.txt #p1");
  });
});
</script>
</head>
<body>

<div id="div1"><h2>使用 jQuery AJAX 修改文本</h2></div>
<button>獲取外部文本</button>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例

可選的 callback 參數(shù)規(guī)定當(dāng) load() 方法完成后所要允許的回調(diào)函數(shù)。回調(diào)函數(shù)可以設(shè)置不同的參數(shù):

responseTxt - 包含調(diào)用成功時(shí)的結(jié)果內(nèi)容statusTXT - 包含調(diào)用的狀態(tài)xhr - 包含 XMLHttpRequest 對(duì)象

下面的例子會(huì)在 load() 方法完成后顯示一個(gè)提示框。如果 load() 方法已成功,則顯示"外部?jī)?nèi)容加載成功!",而如果失敗,則顯示錯(cuò)誤消息:

實(shí)例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("/try/ajax/demo_test.txt",function(responseTxt,statusTxt,xhr){
      if(statusTxt=="success")
        alert("外部?jī)?nèi)容加載成功!");
      if(statusTxt=="error")
        alert("Error: "+xhr.status+": "+xhr.statusText);
    });
  });
});
</script>
</head>
<body>

<div id="div1"><h2>使用 jQuery AJAX 修改該文本</h2></div>
<button>獲取外部?jī)?nèi)容</button>

</body>
</html>

運(yùn)行實(shí)例 ?

點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例