jQuery中的jQuery.load()和jQuery.ajax()方法有什么区别?
jQueryajax()方法
jQuery.ajax(options)方法使用HTTP请求加载远程页面。$.ajax()返回它创建的XMLHttpRequest。在大多数情况下,您不需要该对象直接进行操作,但是如果您需要手动中止请求,则可以使用该对象。
这是此方法使用的所有参数的描述-
options- 一组配置Ajax请求的键/值对。所有选项都是可选的。
假设我们在result.html 文件中具有以下HTML内容-
<h1>THIS IS RESULT...</h1>
示例
以下是显示此方法用法的示例。在这里,我们利用成功处理程序填充返回的HTML-
<head>
<script src = "https://cdn.staticfile.org/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>jQueryload()方法
load(url,data,callback)方法从服务器加载数据,并将返回的HTML放入匹配的元素中。
这是此方法使用的所有参数的描述-
url- 一个包含请求发送到的URL的字符串。
data- 此可选参数表示与请求一起发送的数据映射。
callback- 此可选参数表示如果请求成功执行的功能。
假设我们在result.html文件中包含以下HTML内容-
<h1>THIS IS RESULT...</h1>
示例
以下是显示此方法用法的代码段。
<head>
<script src = "https://cdn.staticfile.org/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>