When using jQuery .each(), is it possible to use a

2019-06-20 14:08发布

I have this code block I find particularly long and hard to udnerstood : the call stack is full of implicit functions and paramters implicitely added to it. in other words, i would like to clarify my code by separating the function called in the each from the each itself.

Look that example :

$(xml).find('group').each(function () {
    var groupName = $(this).attr('name');
    // There is here around 100 lines of codes I would like to split in 
    // at least five functions, And I'm sure it is possible to use named functions
    // instead of implicit ones, no ?

2条回答
Luminary・发光体
2楼-- · 2019-06-20 14:33

You could also just do:

$(xml).find('group').each(function(){
    yourFunction();
});
查看更多
我想做一个坏孩纸
3楼-- · 2019-06-20 14:51

Try passing function reference

Live Demo

$(xml).find('group').each(myfun);

function myfun(i, item)
{
    alert(item.id);
}
查看更多
登录 后发表回答