jQuery ajax - load() 步骤 回调函数
jQuery ajax - load() 方法 回调函数
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("div").load("wrongname.xml",function(response,status,xhr){
if (status=="success")
{
$("div").html("<ol></ol>");
$(response).find("artist").each(function(){
var item_text = $(this).text();
$('<li></li>').html(item_text).appendTo('ol');
});
}
else
{
$("div").html("An error occured: <br/>" + xhr.status + " " + xhr.statusText)
}
});
});
});
</script>
</head>
<body>
<p>Artists</p>
<div></div>
<button>获得 CD 信息</button>
<p>本例中使用的 XML 文件是 <a href="/example/xmle/cd_catalog.xml" target="_blank">cd_catalog</a></p>
</body>
</html>
jQuery ajax - load() 方法
jQuery Ajax 参考手册
实例
使用 AJAX 请求来改变 div 元素的文本:
$("button").click(function(){ $("div").load('demo_ajax_load.txt'); });
亲自试一试
您可以在页面底部找到更多 TIY 实例
定义和用法
load() 方法通过 AJAX 请求从服务器加载数据,并把返回的数据放置到指定的元素中。
注释:还存在一个名为 load 的 jQuery 事件方法。调用哪个,取决于参数。
语法
load(url,data,function(response,status,xhr))
url | 规定要将请求发送到哪个 URL。 |
data | 可选。规定连同请求发送到服务器的数据。 |
function(response,status,xhr) |
可选。规定当请求完成时运行的函数。 额外的参数:
|
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("div").load("wrongname.xml",function(response,status,xhr){
if (status=="success")
{
$("div").html("<ol></ol>");
$(response).find("artist").each(function(){
var item_text = $(this).text();
$('<li></li>').html(item_text).appendTo('ol');
});
}
else
{
$("div").html("An error occured: <br/>" + xhr.status + " " + xhr.statusText)
}
});
});
});
</script>
</head>
<body>
<p>Artists</p>
<div></div>
<button>获得 CD 信息</button>
<p>本例中使用的 XML 文件是 <a href="/example/xmle/cd_catalog.xml" target="_blank">cd_catalog</a></p>
</body>
</html>