【问题标题】:use ajaxStart and ajaxStop jquery methods only for some of requests仅对某些请求使用 ajaxStart 和 ajaxStop jquery 方法
【发布时间】:2011-06-15 00:06:57
【问题描述】:

当请求通过 Ajax 发送时,我使用 AjaxStart 和 AjaxStop jquery 方法来显示和隐藏加载消息。像下面的代码:

                $('<div><img src="images/searching.gif" align="absmiddle" border="0" />Please Wait ...</div>')
            .attr('id','loading')
            .appendTo('body')
                .ajaxStart(function() { 
                  $(this).animate({
                        top     : '40px',
                        opacity : 1.0
                   }, 500);
                })
                .ajaxStop( function(){ 
                  $(this).animate({
                       top     : '-75px',
                       opacity : 0.1
                   }, 500);
                });

但我不希望所有请求都显示此加载,而只显示某些特定请求。 你有解决问题的办法吗?

【问题讨论】:

    标签: jquery ajax


    【解决方案1】:

    分离代码行以创建加载器图像并绑定 ajax 事件

    区分需要 ajaxStop/ajaxStart 事件的图像和不需要的图像。使用 CSS 类来区分图像。

    为 ajax 事件创建加载器图像:

    对于 ajax:

    $('<div><img src="images/searching.gif" align="absmiddle" border="0" />Please Wait ...</div>').attr('id','loading').attr('css','ajax-img').appendTo('body')
    

    不需要ajax:

    $('<div><img src="images/searching.gif" align="absmiddle" border="0" />Please Wait ...</div>').attr('id','loading').appendTo('body')
    

    将您的 ajaxStop 和 ajaxStart 事件绑定到具有“ajax-img”类的图像

    $('img.ajax-img').ajaxStart(function() { 
                      $(this).animate({
                            top     : '40px',
                            opacity : 1.0
                       }, 500);
                    })
                    .ajaxStop( function(){ 
                      $(this).animate({
                           top     : '-75px',
                           opacity : 0.1
                       }, 500);
                    });
    

    【讨论】:

    • 谢谢。但这不是我的答案。此图像显示为加载消息,而不是“请稍候!”例如消息。但是这个加载消息会针对所有 ajax 请求显示,而我希望它不会针对某些特定请求显示
    猜你喜欢
    • 2012-09-18
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多