2013-04-02 68 views
2

當我發送jQuery時,我想通過ajaxStart()顯示加載gif。完成後,gif應該使用ajaxStop()消失。Internet Explorer和ajaxStart/ajaxStop

$(document).ajaxStart(function() { 
$('#info').show(); 

}).ajaxStop(function() { 
$('#info').hide(); 
}); 

它可以在Firefox上正常工作,但不在IE中(我使用的是版本8)。看來,IE在查詢結束時執行ajaxStart(),並且在執行ajaxStop()之後。如果我註釋掉$('#info').hide();部分,我會在看到數據後看到gif。

我可以通過設置async=true來避開它,但我不想這樣做。有另一種方法嗎?

我閱讀了一些文章,指出很多人都有這個問題,但我找不到答案。

編輯:

我認識到,ajaxStart正確執行,但請求會導致瀏覽器凍結,直到我得到迴應。這發生得如此之快,以至於無法顯示加載gif。在ajaxStart和gif顯示之後,用alert()試了一下。之後,alert彈出,然後發送請求,導致瀏覽器和gif凍結。

+0

你使用的是什麼jQuery版本? – Spudley

+0

我使用的版本1.7.2 –

回答

1

儘量使用display屬性顯示/隱藏DIV。

HTML

<div id="loading">&nbsp;</div> 

CSS

#loading { 
    display: none; 
    background: rgba(255, 255, 255, .6) 
      url(../img/loading.gif) 
      50% 50% 
      no-repeat; 
    width: 100%; 
    height: 100%; 
    position: fixed; 
    top: 0; 
    left: 0; 
} 

jQuery的

$(document).on({ 
    ajaxStart: function() { 
     $('#loading').css('display', 'block') 
    }, 
    ajaxStop: function() { 
     $('#loading').css('display', 'none') 
    } 
}); 
1

我有同樣的問題。加載div顯示在Firefox中,但不顯示在ie8和ie9。我所做的只是添加了setTimeout間隔及其作品。

首先檢查瀏覽器是否爲IE8或IE9。在你的HTML補充一點:

<!--[if IE 8]><html lang="en-us" class="ie ie8"><![endif]--> 
<!--[if IE 9]><html lang="en-us" class="ie ie9"><![endif]--> 
<!--[if gt IE 9]><!--> <html lang="en-us"><!--<![endif]--> 
<head> 

如果是IE8或IE9添加的間隔,如果不是沒有。

$(document).ready(function() { 
    var ie8or9 = $('html').hasClass('ie8') || $('html').hasClass('ie9'); 
    $(document).ajaxStart(function() { 
     setTimeout(function(){ 
      $('#loading').show(); 
     }, ie8or9 ? 50 : 0); 
    }).ajaxStop(function() { 
     setTimeout(function(){ 
      $('#loading').hide(); 
     }, ie8or9 ? 100 : 0); 
    }); 
}); 
+0

感謝您的提示。在ie11或Chrome中不適合我。正如您發現的那樣,您可以很好地使用Firefox – mattpm