jQuery 每次回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12264388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:29:58  来源:igfitidea点击:

jQuery each callback

jquerycallbackeach

提问by NoNameZ

How to use callbacks on jQuery eachfunction?

如何在jQuery 每个函数上使用回调?

I am trying something like:

我正在尝试类似的东西:

$.each(images, function(key, value) { 
    new_images+= '<li><a href="'+value+'"><img src="'+value+'" alt="'+[key]+'" /></a></li>';
}, function (){
    $('#Gallery').remove();
    $('body').append('<ul class="gallery">'+new_images+'</ul>');
});

回答by Damian SIlvera

$.each();is a synchronous function. That means you don't need a callback function inside because any code you write after $.each();will run affter $.each();ends.

$.each();是一个同步函数。这意味着您不需要内部的回调函数,因为您之后编写的任何代码$.each();都会在$.each();结束后运行。

回答by h2ooooooo

What's wrong with this code? You don't need a callback for $.each.

这段代码有什么问题?您不需要回调$.each.

$.each(images, function(key, value) { 
    new_images+= '<li><a href="'+value+'"><img src="'+value+'" alt="'+[key]+'" /></a></li>';
});

$('#Gallery').remove();
$('body').append('<ul class="gallery">'+new_images+'</ul>');

回答by Arsen Mkrtchyan

Do you mean this?

你是这个意思吗?

$.each(images, function(key, value) { 
    new_images+= '<li><a href="'+value+'"><img src="'+value+'" alt="'+[key]+'" /></a></li>';
});
function myMethod(){
    $('#Gallery').remove();
    $('body').append('<ul class="gallery">'+new_images+'</ul>');
};
myMethod();

回答by FishWave

I have now a solution for an .each-callback!

我现在有一个 .each-callback 的解决方案!

I had to do an ajax-request for each element in an array. So I made a div-container with all the div-elements for each element. Each time a load was made, the div-element changed to green and .slideUp, after .remove. And each time i asked if the div-container is empty. If yes, I know, all elements are fully loades (because removed).

我必须对数组中的每个元素执行 ajax 请求。所以我用每个元素的所有 div 元素制作了一个 div 容器。每次加载时,div 元素在 .remove 之后变为绿色和 .slideUp。每次我问 div 容器是否为空时。如果是,我知道,所有元素都已满载(因为已删除)。

Here a part of my code:

这是我的代码的一部分:

<script>
$(document).ready( function() {
    $.each(myChannels, function(index, value) {
        $("#tag_"+value).load('getxmls/ajaxrenewchannel/channel:'+value, function() {
            $("#tag_"+value).removeClass('alert-info').addClass('alert-success');
            $("#tag_"+value).slideUp('600', function () {
                $("#tag_"+value).remove();
                if( $('#loadcontainer').is(':empty') ) {
                    window.location = 'feeds/';
                }

                });

        });
    });
});
</script>

Hope this helps somebody out there...

希望这可以帮助那里的人...

回答by Shyju

Not sure about the callback you meant. I guess this is what you are looking for

不确定你的意思是回调。我想这就是你要找的

$('#Gallery').remove();
var selectItems='<ul class="gallery">';
$.each(images, function(key, value) { 
    selectItems+= '<li><a href="'+value+'">
                              <img src="'+value+'" alt="'+[key]+'" /></a></li>';    
});   
selectItems+= '</ul>';
$('body').append(selectItems);