在页面加载时显示div,完成后隐藏
答
$.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
答
$.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();
}
});
答
@cballou您的代码将离开 '#div' “起来”,如果$。阿贾克斯()尚未suceeded任何的许多可能的原因。
+0
注意和修复。 – 2009-12-02 20:08:47
答
几乎正确;) 绝不低估删除冗余$()调用的重要性。所以......
//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
如何将XML内容加载?它是通过AJAX吗? – rahul 2009-11-05 13:39:47