正在尝试显示正在加载的图像
Trying to display a loading image
我已经建议了问题行,所以显示工作代码:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#btnGet').click(function() {
get_conflicts( $("#txtValue").val() );
});
$("#txtValue").live('keyup', function()
{
if ($("#txtValue").val().length > 3) {
get_conflicts( $("#txtValue").val() );
} else {
$("#divResults").empty();
}
});
function get_conflicts( phrase ) {
$.ajax({
type: 'POST',
url: 'conflict.asmx/GetConflicts',
data: '{phrase: "' + phrase + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
beforeSend: function() {
$('#spanLoading').empty().append("<img src='/img/loading.gif' />");
},
success: function( conflicts ) {
$("#divResults").empty();
if( conflicts.d[0] ) {
$.each( conflicts.d, function( index, conflict ) {
$("#divResults").append( conflict.Group + ':' + conflict.Count + '<br />' );
});
} else {
alert( "null" );
}
},
complete: function() {
$('#spanLoading').empty();
},
error: function(xhr, status, error) {
$('#spanLoading').empty();
var err = eval("(" + xhr.responseText + ")");
alert(err.Message) ;
}
});
}
});
</script>
</head>
<body>
<form id="form1" runat="server"></form>
<input type="button" id="btnGet" value="Get" /><br />
<input type="text" id="txtValue" /> <span id="spanLoading" /><br />
<div id="divResults" />
</body>
</html>
如果我取消注释第一个推荐的输出行,为什么这段代码会停止将结果打印到屏幕上?
这些是全局设置。所有ajax调用都显示一个加载图像。
$(".loading").ajaxStart(function () {
$(this).delay(500).slideDown(200);
});
$(".loading").ajaxComplete(function () {
$(this).delay(500).fadeOut(200);
}
<div class="loading" style="display: none">
<div>
<img src="/img/loading.gif" title="Loading" alt="Loading" />
Please Wait. Loading....
</div>
</div>
您可以使用ajax请求的beforeSend
和complete
事件。
$.ajax({
beforeSend:function(data){
//Show Image
},
complete: function(data){
//Hide Image
},
//rest of your code
});
使用ajaxSetup
$.ajaxSetup({
beforeSend:function(){
//show loading div
},
complete:function(){
//remove the loading div
}
});
相关文章:
- 在通过child.print()打印之前,我如何等待图像加载到我的新窗口中
- 在检索数据时是否可以停止图像加载请求
- 如何让我的网站上的WEBP图像加载在morzilla firefox中有一个可能的解决方案吗?如果是,那么如何
- jQuery:获取图像加载错误的详细信息
- 在safari和chrome中,js图像加载产生了奇怪的结果
- 图像加载不适用于 IE 8 或更低版本
- 这个图像加载功能有什么作用
- JavaScript 图像加载器不起作用
- 动态图像加载是否会减少服务器过载
- 跨来源图像加载被拒绝-仅限iPhone-简单图像上传
- 纯javascript的进度图像加载程序仅当用户单击提交html表单时
- 使用JavaScript延迟图像加载
- 如果当前图像加载缓慢,如何加载其他图像
- HTML图像加载触发一个错误
- 将图像加载到localStorage,并将图像src设置到该位置
- jquery图像加载检查错误
- 将图像加载到织物画布背景中
- 需要在bootstrap css中单击时将图像加载为模态
- 正在显示加载..在将新图像加载到网站时播放动画
- 砖石事件:在图像加载和布局完成后调用事件