我有一个功能:
function removeDiv() {
var topmost = jQuery('.xx');
var totContent = topmost.find('.zz').length;
var $target = jQuery('.xx').find('.zz').eq(0);
if(totContent > 5) {
$target.hide('slow', function(){ $target.remove(); });
}
}
我在ajax调用中使用它,以删除多余的div,然后有5个以上,但是它仅删除一次div一次!
这是ajax调用的外观:
function saveClubs(array) {
for(i=0; i<array.length; i++) {
var id = array[i];
jQuery.ajax({
type: "GET",
async: true,
url: 'index.php?option=com_events&task=club.save&id=' + id,
dataType: 'json',
success: function(data) {
jQuery('.xx').append('<div class="zz">'+data+'</div>');
removeDiv();
}
});
}
}
有任何想法吗 ?
这是保罗·鲁布(Paul Roub)的答案,发布为答案,而不是评论:
可能的问题是,由于您要在一个循环中进行大量ajax调用,因此它们往往会同时完成,因此您最终会重复淡出相同的元素(因为它仍然存在直到完成淡入淡出)。
在最小的变化解决将是,比如说,添加为你逐渐把它列一类:
function removeDiv() {
// Get the container (I take it there's only one .xx element)
var topmost = jQuery('.xx');
// Get the child elements that aren't fading
var zz = topmost.find('.zz').not('.fading');
// Too many?
if(zz.length > 5) {
// Yup, add 'fading' to the first one and fade it out
// Note that there's no need for the $target variable
zz.eq(0).addClass('fading').hide('slow', function(){ $(this).remove(); });
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句