2009-11-05 160 views

回答

8
$.ajax({ 
    url: '/test.xml', 
    beforeSend: function(XMLHttpRequest) { 
     // Show the div before sending the request 
     $('#load').show(); 
    }, 
    complete: function(XMLHttpRequest, textStatus) { 
     // Hide the div no matter if the call succeeded or not 
     $('#load').hide(); 
    }, 
    success: function(xml) { 
     // if the request succeeds do something with the received XML   
    } 
}); 
+0

为什么这是“最好的”解决方案?矿井几乎完全相同并具有“0”。不是我在乎,而是让我想知道这里的“得分系统”吗? – 2011-02-04 00:40:42

3
$.ajax({ 
    type: "GET", 
    url: "your.xml", 
    dataType: "xml", 
    beforeSend: function() { 
     $('#div').fadeIn(); 
    }, 
    success: function(xml) { 
     // example for parsing xml 
     $(xml).find('YOUR_XML_TAG').each(function(){ 
      // append xml to page HERE 
     }); 
    }, 
    complete: function() { 
     $('#div').fadeOut(); 
    } 
}); 
2

@cballou您的代码将离开 '#div' “起来”,如果$。阿贾克斯()尚未suceeded任何的许多可能的原因。

+0

注意和修复。 – 2009-12-02 20:08:47

0

我会在页面URL更改时使用onbeforeunload事件来创建一个覆盖div,其中不透明度为0.5,当页面加载时将被新内容替换。

1

几乎正确;) 绝不低估删除冗余$()调用的重要性。所以......

//all of this is inside some closure or function 
var $blanket = $("#div") ; 
// check if after last call, something has possibly removed your '#div' 
// throw if false 
ASSERT($blanket.length === 1) ;  
$.ajax({ 
     type: "GET", 
     url: "your.xml", 
     dataType: "xml", 
     beforeSend: function() { $blanket.fadeIn(); 
     }, 
     success: function(xml) { 
      // example for parsing xml 
      $(xml).find('YOUR_XML_TAG').each(function(){ 
       // append xml to page HERE 
      }); 
     }, 
     complete: function() { $blanket.fadeOut(); 
     } 
    }); 

--DBJ

相关问题