load() 从服务器加载数据,并把返回的数据放入被选元素中。
在[color=red]不重载[/color]全部页面的情况下,实现了对部分网页的[color=red]更新[/color]。
语法:$(selector).load(URL,data,callback(responseTxt,statusTxt,xhr));
必需的 URL 参数规定您希望加载的 URL。
可选的 data 参数规定与请求一同发送的查询字符串键/值对集合。
可选的 callback 参数是 load() 方法完成后所执行的函数名称。
回调函数callback的参数:
responseTxt - 包含调用成功时的结果内容
statusTXT - 包含调用的状态
xhr - 包含 XMLHttpRequest 对象
实例:把文件 "demo_test.txt" 的内容加载到指定的 <div> 元素中:
"demo_test.txt"的内容:
<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>
[img]http://dl2.iteye.com/upload/attachment/0105/4967/df247a47-003f-31ec-9ea1-cf08679c6874.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0105/4963/fac6d94d-58ef-33b6-a37f-56ff2f1d391b.jpg[/img]
在[color=red]不重载[/color]全部页面的情况下,实现了对部分网页的[color=red]更新[/color]。
语法:$(selector).load(URL,data,callback(responseTxt,statusTxt,xhr));
必需的 URL 参数规定您希望加载的 URL。
可选的 data 参数规定与请求一同发送的查询字符串键/值对集合。
可选的 callback 参数是 load() 方法完成后所执行的函数名称。
回调函数callback的参数:
responseTxt - 包含调用成功时的结果内容
statusTXT - 包含调用的状态
xhr - 包含 XMLHttpRequest 对象
实例:把文件 "demo_test.txt" 的内容加载到指定的 <div> 元素中:
"demo_test.txt"的内容:
<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>
[img]http://dl2.iteye.com/upload/attachment/0105/4967/df247a47-003f-31ec-9ea1-cf08679c6874.jpg[/img]
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("jquery/demo_test.txt",function(responseTxt,statusTxt,xhr){
if(statusTxt=="success")
alert("外部内容加载成功!");
if(statusTxt=="error")
alert("Error: "+xhr.status+": "+xhr.statusText);
});
});
});
</script>
</head>
<body>
<h3 id="test">请点击按钮通过 jQuery AJAX 改变这段文本。</h3>
<button id="btn1" type="button">获得外部的内容</button>
</body>
[img]http://dl2.iteye.com/upload/attachment/0105/4963/fac6d94d-58ef-33b6-a37f-56ff2f1d391b.jpg[/img]