Javascript jQuery“each”速记

Javascript jQuery“each”速记,javascript,jquery,html,each,Javascript,Jquery,Html,Each,有没有什么方法可以让我速记每个jquery?或者在编写jquery时是否有比下面代码更短的替代方法 $('someElement').each(function () { functionName('value1', value2) }); $('anotherElement').each(function () { functionName('value1', value2) }); $('andMoreElement').each(function () { func

有没有什么方法可以让我速记每个jquery?或者在编写jquery时是否有比下面代码更短的替代方法

$('someElement').each(function () {
    functionName('value1', value2)
});
$('anotherElement').each(function () {
    functionName('value1', value2)
});
$('andMoreElement').each(function () {
    functionName('value1', value2)
});

functionName(foo, element){
   //function code here
}

假设要展开选择,您可能会对.add函数感兴趣:

$('someElement').add('onotherElement').each(function() { })
将您的逻辑应用于someElement的所有匹配项以及问题最新版本中的OnTheElement,其中三个元素都使用相同的值,最好使用逗号系列的选择器(如果它们是选择器),或者使用add函数(如果不是):

$('someElement, anotherElement, andMoreElement').each(function () {
    functionName('value1', value2)
});

functionName(foo, element){
   //function code here
}

同样,这取决于“someElement”、“anotherElement”等是选择器还是元素

因为您正在使用each,它会立即调用函数,所以您也可以使用下面的curry选项。如果您使用的是click或类似工具,则在计算value2时,使用下面的curry选项将从调用函数时更改为curry时,这可能是可取的,也可能是不可取的,具体取决于您的用例

回答问题的早期版本:

遗憾的是,您不能使用$.proxy或Functionbind进行此操作,因为它们都会在调用中更改此的值。您可以创建一个可重用调用它时使用的:

var slice = Array.prototype.slice;
function curry(f) {
    var args = slice.call(arguments, 1); // Copy args after `f`
    return function() {
        return f.apply(this, args.concat(slice.call(arguments, 0)));
    };
}
当您将一个函数和X个参数传递到curry中时,它将返回一个函数,调用该函数时,该函数将使用调用该函数时使用的值、提供给curry的参数以及提供给调用的任何参数调用原始函数

然后:

或者,由于后两种用法具有相同的参数:

$('someElement').each(curry(functionName, 'foo'));
$('anotherElement, andMoreElement').each(curry(functionName, 'otherFoo'));
// Or:
$('anotherElement').add('andMoreElement').each(curry(functionName, 'otherFoo'));

functionName(foo){
   //function code here
}

编辑:这将允许您根据问题中的编辑传递参数

var a = 'Function', b = 'works';

$('.a').each(foo.bind(this, a + 'foo', b + 'foo')); // (1)

function foo(a, b, i, el) {
  console.log(a, b, $(el).text());
}

1我将foo添加到变量中,以证明foo没有拾取全局变量,而是传入的参数。

为它们提供相同的类,如$'.thesameClass'.eachfunction{};你想实现什么?什么是foo和其他foo?它们来自哪里?您可以将它们放入元素的数据属性中,然后只调用一个循环,将数据属性传递到functionName$'someElement'。如果函数不需要任何参数,则每个functionName都会更短。您会不断更改问题。请注意,每当你改变问题时,你都会使人们友好地提供的答案无效。这并不是你认为它做了什么/OP想要什么。在某个元素中查找一个元素。请参阅。每次调用functionName时,参数foo和元素都有一个唯一的值。所以每次我在每个语句中调用functionName时,值很可能与其他值不同。我想知道是否有任何方法可以像你一样速记每个if条件语句不它不在某个元素中查找ontehrelement。。。你可以从页面的任何部分添加你想要的元素。每次都会调用每个元素。它会在另一个元素中查找一个元素。要查找任一元素,必须在字符串中传递一个带逗号的字符串,如下所示:$'OnTherement,someElement'@user2256955:否它不再在someElement中查找onteherelement:请阅读文档。如果你不相信这一点,也许举个例子会有所帮助:@JetzKi:很高兴这有帮助。嗨,t.J.作为一名职业选手,我非常钦佩,如果你认为我的问答有必要,我将非常感谢你的评论:
$('someElement').each(curry(functionName, 'foo'));
$('anotherElement, andMoreElement').each(curry(functionName, 'otherFoo'));
// Or:
$('anotherElement').add('andMoreElement').each(curry(functionName, 'otherFoo'));

functionName(foo){
   //function code here
}
 $('#onotherElement','#someElement').each(function () {
        functionName('foo')
    });
var a = 'Function', b = 'works';

$('.a').each(foo.bind(this, a + 'foo', b + 'foo')); // (1)

function foo(a, b, i, el) {
  console.log(a, b, $(el).text());
}