小白教程
所有教程
关于
Search
172.70.130.177
172.70.130.177
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“JQuery – AJAX load() 方法”的源代码
本页内容
上一节:
JQuery_AJAX_简介
下一节:
JQuery_–_AJAX_get()_和_post()_方法
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:jQuery load() 方法}}[[Category:jQuery 教程|27]] = jQuery - AJAX load() 方法 = == jQuery load() 方法 == jQuery load() 方法是简单但强大的 AJAX 方法。 load() 方法从服务器加载数据,并把返回的数据放入被选元素中。 '''语法:''' <sample title="" desc="" lang="html" hererun="1"> $(selector).load(URL,data,callback); </sample> 必需的 URL 参数规定您希望加载的 URL。 可选的 data 参数规定与请求一同发送的查询字符串键/值对集合。 可选的 callback 参数是 load() 方法完成后所执行的函数名称。 这是示例文件("demo_test.txt")的内容: <sample title="" desc="" lang="html" hererun="1"> <h2>jQuery AJAX 是个非常棒的功能!</h2> <p id="p1">这是段落的一些文本。</p> </sample> 下面的例子会把文件 "demo_test.txt" 的内容加载到指定的 <div> 元素中: <sample title="" desc="" lang="html" hererun="1"> $("#div1").load("demo_test.txt"); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <script src="https://cdn.staticfile.org/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 修改文本内容</h2></div> <button>获取外部内容</button> </body> </html> </run> 也可以把 jQuery 选择器添加到 URL 参数。 下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载到指定的 <div> 元素中: <sample title="" desc="" lang="html" hererun="1"> $("#div1").load("demo_test.txt #p1"); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/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> </run> 可选的 callback 参数规定当 load() 方法完成后所要允许的回调函数。回调函数可以设置不同的参数: * responseTxt - 包含调用成功时的结果内容 * statusTXT - 包含调用的状态 * xhr - 包含 XMLHttpRequest 对象 下面的例子会在 load() 方法完成后显示一个提示框。如果 load() 方法已成功,则显示"外部内容加载成功!",而如果失败,则显示错误消息: <sample title="" desc="" lang="html" hererun="1"> $("button").click(function(){ $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){ if(statusTxt=="success") alert("外部内容加载成功!"); if(statusTxt=="error") alert("Error: "+xhr.status+": "+xhr.statusText); }); }); </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/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("外部内容加载成功!"); if(statusTxt=="error") alert("Error: "+xhr.status+": "+xhr.statusText); }); }); }); </script> </head> <body> <div id="div1"><h2>使用 jQuery AJAX 修改该文本</h2></div> <button>获取外部内容</button> </body> </html> </run>
返回至“
JQuery – AJAX load() 方法
”。
上一节:
JQuery_AJAX_简介
下一节:
JQuery_–_AJAX_get()_和_post()_方法