Javascript 正在尝试显示正在加载的图像

Javascript 正在尝试显示正在加载的图像,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我已推荐了问题行,以便显示工作代码: <%@ 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:

我已推荐了问题行,以便显示工作代码:

<%@ 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>

无标题页
$(文档).ready(函数(){
$('#btnGet')。单击(函数(){
获取冲突($(“#txtValue”).val());
});
$(“#txtValue”).live('keyup',function())
{ 
if($(“#txtValue”).val().length>3){
获取冲突($(“#txtValue”).val());
}否则{
$(“#divResults”).empty();
}
});
函数get_冲突(短语){
$.ajax({
键入:“POST”,
url:'conflict.asmx/GetConflicts',
数据:“{短语:“+短语+”}”,
contentType:'application/json;charset=utf-8',
数据类型:“json”,
beforeSend:function(){
$('#spanLoading').empty().append(“”);
},
成功:功能(冲突){
$(“#divResults”).empty();
if(冲突.d[0]){
$.each(冲突.d,函数(索引,冲突){
$(“#divResults”).append(conflict.Group+”:“+conflict.Count+”
); }); }否则{ 警报(“空”); } }, 完成:函数(){ $('#spanLoading').empty(); }, 错误:函数(xhr、状态、错误){ $('#spanLoading').empty(); var err=eval(“+xhr.responseText+”); 警报(错误消息); } }); } });


如果我取消注释第一行,为什么此代码会停止将结果打印到屏幕上?

您可以在发送前使用
完成ajax请求的事件

 $.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
}

});

这些用于全局设置。所有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>
$(.loading”).ajaxStart(函数(){
$(此).delay(500).slideDown(200);
});
$(“.loading”).ajaxComplete(函数(){
延迟(500),衰减(200);
}
请稍候,正在加载。。。。

我在您的代码中看不到此元素:span TrafficAlertLoadingtry not。span不能自封闭element@Yorgo,就是这样,跨度不对。谢谢。