jQuery - AJAX load() 方法

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]

<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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值