如何在javascript变量中存储多个jQuery选择器?

时间:2022-12-02 17:14:16

Obviously it's a good idea to store jQuery selectors in variables if they are used more than once (not a good idea if used only once).

显然,如果jQuery选择器被多次使用,那么将它们存储在变量中是个好主意(如果只使用一次,这不是一个好主意)。

My question is, how do you store multiple selectors, that are used interchangeably, in a variable.

我的问题是,如何在变量中存储可互换使用的多个选择器。

For instance, let's say I select $('#object1, #object2), then later on I select `$('#object1'). How would I create a variable that can be combined with other variables to create multiple selectors.

例如,假设我选择$('#object1,#object2),然后我选择`$('#object1')。如何创建可与其他变量组合以创建多个选择器的变量。

If I were to write it like this:

如果我这样写:

var object1 = "#object1";
var object2 = "#object2";

$(object1  + "," + object2);

I would only be referencing the string #object1, and not the actual selector.

我只会引用字符串#object1,而不是实际的选择器。

So how do I store multiple selectors in variables, so they can be used interchangeably?

那么如何在变量中存储多个选择器,以便它们可以互换使用?

5 个解决方案

#1


8  

The problem is not the selector string, the problem is the query of the DOM element. "Problem" means, it's expensive. That is why you should only query an element once and store a reference to it.

问题不在于选择器字符串,问题在于DOM元素的查询。 “问题”意味着,它很昂贵。这就是为什么你应该只查询一次元素并存储对它的引用。

var object1 = $('#object1'),
    object2 = $('#object2');

object1.some_method();

update

更新

In reference to your comment:

参考你的评论:

jQuery offers the .add()help method which allows to concat jQuery objects:

jQuery提供了.add()帮助方法,它允许连接jQuery对象:

object1.add(object2).method();

#2


3  

Not completely sure I understand what you mean. You want to store the jQuery objects so you don't have to continually search for them like some sort of cache?

不完全确定我明白你的意思。你想存储jQuery对象,所以你不必像某种缓存那样不断地搜索它们吗?

var cache = new Object();
cache['#object1'] = $('#object1');
cache['#object2'] = $('#object2');

To retrieve the jQuery values, you'd merely have to call cache['#object1'].

要检索jQuery值,您只需调用cache ['#object1']。

#3


1  

You can use .add():

你可以使用.add():

var object1 = $('#object1'), object2 = $('#object2');

object1.add(object2).addClass('couple');

Or,

要么,

var multiple = [object1[0], object2[0], ...];

$(multiple).addClass('party');

#4


0  

a selector is a string when passed to the $() fn. returns a collection of Elements that match the selector. So there is nothing wrong with this code.

传递给$()fn时,selector是一个字符串。返回与选择器匹配的Elements集合。所以这段代码没有任何问题。

Your code can also be written as,

您的代码也可以写成,

var object1 = "#object1",
    object2 = "#object2";

$(object1).add(object2).doSomething();

or it can be further optimized as,

或者可以进一步优化,

var object1 = $('#object1'),
    object2 = $('#object2');

object1.add(object2).doSomething();

where doSomething can be a jQuery or jQuery plugin defined method.

doSomething可以是jQuery或jQuery插件定义的方法。

#5


0  

Why don't you use the easy way.

你为什么不用简单的方法。

var severalVar = 'h1,h2,h3,h4,h5,h6';
 $(severalVar).hide()

#1


8  

The problem is not the selector string, the problem is the query of the DOM element. "Problem" means, it's expensive. That is why you should only query an element once and store a reference to it.

问题不在于选择器字符串,问题在于DOM元素的查询。 “问题”意味着,它很昂贵。这就是为什么你应该只查询一次元素并存储对它的引用。

var object1 = $('#object1'),
    object2 = $('#object2');

object1.some_method();

update

更新

In reference to your comment:

参考你的评论:

jQuery offers the .add()help method which allows to concat jQuery objects:

jQuery提供了.add()帮助方法,它允许连接jQuery对象:

object1.add(object2).method();

#2


3  

Not completely sure I understand what you mean. You want to store the jQuery objects so you don't have to continually search for them like some sort of cache?

不完全确定我明白你的意思。你想存储jQuery对象,所以你不必像某种缓存那样不断地搜索它们吗?

var cache = new Object();
cache['#object1'] = $('#object1');
cache['#object2'] = $('#object2');

To retrieve the jQuery values, you'd merely have to call cache['#object1'].

要检索jQuery值,您只需调用cache ['#object1']。

#3


1  

You can use .add():

你可以使用.add():

var object1 = $('#object1'), object2 = $('#object2');

object1.add(object2).addClass('couple');

Or,

要么,

var multiple = [object1[0], object2[0], ...];

$(multiple).addClass('party');

#4


0  

a selector is a string when passed to the $() fn. returns a collection of Elements that match the selector. So there is nothing wrong with this code.

传递给$()fn时,selector是一个字符串。返回与选择器匹配的Elements集合。所以这段代码没有任何问题。

Your code can also be written as,

您的代码也可以写成,

var object1 = "#object1",
    object2 = "#object2";

$(object1).add(object2).doSomething();

or it can be further optimized as,

或者可以进一步优化,

var object1 = $('#object1'),
    object2 = $('#object2');

object1.add(object2).doSomething();

where doSomething can be a jQuery or jQuery plugin defined method.

doSomething可以是jQuery或jQuery插件定义的方法。

#5


0  

Why don't you use the easy way.

你为什么不用简单的方法。

var severalVar = 'h1,h2,h3,h4,h5,h6';
 $(severalVar).hide()