使用.add()选择多个jQuery对象 [英] Select multiple jQuery objects with .add()

查看:153
本文介绍了使用.add()选择多个jQuery对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.add() 方法允许一次选择多个对象而不是一次添加一个?

Does the .add() method allow selecting multiple objects in one go instead of adding one at a time?

one.add(two).add(three).add(four).on("click", function() { });

以下变量按其设置方式设置,因为每个变量都带有不同的功能。

The following variables are set in the way they do because each carries a different function.

var one   = $("#1");
var two   = $("#2");
var three = $("#3");
var four  = $("#4");

但是如果我想添加一个适用于所有这些元素的额外函数怎么办?我是否必须逐个添加它们?

But what if I want to add an extra function that applies to all of these elements? Do I have to add them one by one?

我知道您可以使用 $(#1,#2,#3全部选择它们,#4),但我只是想利用上述变量。

I know you can select them all using $("#1,#2,#3,#4"), but I just want to make use of the above variables.

推荐答案

我更喜欢这种数组方法,纯粹是为了便于阅读......

I'd prefer this array approach, purely for readability...

$([one, two, three, four]).each(function() {
    // your function here
});

这篇关于使用.add()选择多个jQuery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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