使用jQuery .each()时,可以使用非匿名函数吗? [英] When using jQuery .each(), is it possible to use a non-anonymous function?

查看:82
本文介绍了使用jQuery .each()时,可以使用非匿名函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码块,我发现它特别长且难以理解:调用堆栈充满了隐式函数和隐式添加到其中的参数.换句话说,我想通过将每个调用的函数与每个自身分开来澄清代码.

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.

看那个例子:

$(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 ?

推荐答案

尝试传递函数参考

实时演示

Live Demo

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

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

这篇关于使用jQuery .each()时,可以使用非匿名函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆