一次在多个jQuery元素上绑定一个函数 [英] Bind a function on multiple jQuery elements at once
本文介绍了一次在多个jQuery元素上绑定一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个jquery对象:
I have 3 jquery objects:
var a = $('.el1');
var b = $('.el2');
var c = $('.el3');
我想一次将更改"事件绑定到所有这些事件,但是我不能:(
And I want to bind a "change" event to all of them at once, but I can't :(
$(a, b, c).bind('paste input change', function(){ ... });
根本不起作用...
$(a, b, c).bind('paste input change', function(){ ... });
simply doesn't work...
但是,如果将其分别绑定到每个元素,它将起作用:
But if bind it to each element separately it works:
a.bind('...');
b.bind('...');
c.bind('...');
是否可以用更短的时间做到这一点?
Is it possible to do this in a shorter way?
(并且不将类作为选择器传递)
(And without passing the classes as selectors)
推荐答案
使用 .add()
[docs] :
a.add(b).add(c).bind(...
这篇关于一次在多个jQuery元素上绑定一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文