jquery与服务器交换数据的利器--ajax(异步javascript and xml)

时间:2014-12-18 21:58:37   收藏:0   阅读:192

load() 方法从服务器加载数据,并把返回的数据放入被选元素中。

一、下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载到指定的 <div> 元素中:

$("#div1").load("demo_test.txt #p1");

参考网址: http://www.w3cschool.cc/jquery/jquery-ajax-load.html

 

二、可选的 callback 参数规定当 load() 方法完成后所要允许的回调函数。回调函数可以设置不同的参数:

下面的例子会在 load() 方法完成后显示一个提示框。如果 load() 方法已成功,则显示"外部内容加载成功!",而如果失败,则显示错误消息:

$("button").click(function(){
  $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
    if(statusTxt=="success")
      alert("External content loaded successfully!");
    if(statusTxt=="error")
      alert("Error: "+xhr.status+": "+xhr.statusText);
  });
});

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!