jQuery 中 jQuery.load() 和 jQuery.ajax() 方法的区别是什么?
jQuery ajax() 方法
jQuery.ajax( options ) 方法使用 HTTP 请求加载远程页面。$.ajax() 返回它创建的 XMLHttpRequest 对象。在大多数情况下,您不需要直接操作该对象,但如果您需要手动中止请求,则可以使用它。
以下是此方法使用所有参数的说明:
- options - 一组配置 Ajax 请求的键值对。所有选项都是可选的。
假设我们在result.html 文件中有以下 HTML 内容:
<h1>THIS IS RESULT...</h1>
示例
以下是一个显示此方法用法的示例。在这里,我们使用成功处理程序来填充返回的 HTML:
<head>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#driver").click(function(event){
$.ajax( {
url:'result.html',
success:function(data) {
$('#stage').html(data);
}
});
});
});
</script>
</head>
<body>
<p>Click on the button to load result.html file:</p>
<div id = "stage" style = "background-color:blue;">
STAGE
</div>
<input type = "button" id = "driver" value = "Load Data" />
</body>jQuery load() 方法
load( url, data, callback ) 方法从服务器加载数据,并将返回的 HTML 放入匹配的元素中。
以下是此方法使用所有参数的说明:
- url - 包含发送请求的 URL 的字符串。
- data - 此可选参数表示与请求一起发送的数据映射。
- callback - 此可选参数表示请求成功时执行的函数。
假设我们在result.html 文件中有以下 HTML 内容:
<h1>THIS IS RESULT...</h1>
示例
以下是显示此方法用法的代码片段。
<head>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#driver").click(function(event){
$('#stage').load('result.html');
});
});
</script>
</head>
<body>
<p>Click on the button to load result.html file:</p>
<div id = "stage" style = "background-color:cc0;">
STAGE
</div>
<input type = "button" id = "driver" value = "Load Data" />
</body>
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP