为了在做Ajax请求时有一个指示器,我得到了一些信息,说我应该在page元素中放置带有动画gif的指示器,然后在ajax函数成功替换数据。添加HTML到带有Ajax的页面元素onclick
如果我添加指标源与src =“ajax-loader.html”,ajax调用将它留在原地并不会将其替换为数据。如果我在.load(“ajax-loader.html”)中添加指示符源,则在ajax调用之前根本不显示它。如果我将它添加到beforesend事件中的ajax调用中,它也不会显示。如果我做两个Ajax调用,一个调用指示器,一个调用这个调用的数据。在这个简单的代码中必须有一种方法来显示指标。
这是页面元素的HTML。
<iframe id="lcupco" style="position: relative; top: 5px; width: 100%; height: 200px; border: 2px solid grey; margin-bottom: 15px;"></iframe>
这是此指示器的HTML。
<html>
<head></head>
<body>
<img src='images/ajax-loader.gif'/>
</body>
</html>
这是代码
调用.load
$(document).ready(function(){ $('#lcupco').load("ajax-loader.html");}); $.ajax({ url: 'luxcal/index.php?cP=40', cache: false, async: true, success: function(data) { $('#lcupco').html(data); }, });
使用beforesend
`
$.ajax({
url: 'luxcal/index.php?cP=40',
cache: false,
async: true,
beforeSend: function() {
$('#lcupco').load('ajax-loader.html');
// $('#lcupco').html('<html>Initializing calendar...</html>'); //simple text didn't load either.
},
success: function(data) {
$('#lcupco').html(data);
},
});
- 使用两个Ajax调用:一个指示器和一个用于数据
`
$.ajax({
url: 'ajax-loader.html',
cache: false,
async: true,
success: function(data) {
$('#lcupco').html(data);
},
});
$.ajax({
url: 'luxcal/index.php?cP=40',
cache: false,
async: true,
beforeSend: function() {
$('#lcupco').html('<html>Initializing calendar...</html>');
},
success: function(data) {
$('#lcupco').html(data);
},
});
`
嗯,这是各种奇怪的。你为什么要更新一个iframe的内容(这似乎没有一个src的,另外,如果你想要去iframe的路线,你能不能更新iframe的src来获取内容(不需要ajax)。另外,你可以在没有调用单独页面的情况下覆盖载入器img吗? – JoeyP 2011-12-27 17:07:24