When using jQuery .each(), is it possible to use a non-anonymous function?

走远了吗. 提交于 2019-12-23 09:36:40

问题


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 ?

回答1:


Try passing function reference

Live Demo

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

function myfun(i, item)
{
    alert(item.id);
}



回答2:


You could also just do:

$(xml).find('group').each(function(){
    yourFunction();
});


来源:https://stackoverflow.com/questions/16086769/when-using-jquery-each-is-it-possible-to-use-a-non-anonymous-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!