使用jQuery .each()时,是否可以使用非匿名函数?
我有这个代码块,我发现特别长,难以理解:调用堆栈充满了隐含的函数和隐含的添加到它的参数。 换句话说,我想通过将每个中调用的函数与每个函数分开来澄清我的代码。
看那个例子:
$(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 ?
尝试传递函数引用
现场演示
$(xml).find('group').each(myfun); function myfun(i, item) { alert(item.id); }
你也可以这样做:
$(xml).find('group').each(function(){ yourFunction(); });