将jquery变量合并到一个选择器中? [英] Combine jquery variables into one selector?
问题描述
我正在寻找一种组合jquery变量的方法.通过相关问题,似乎没有人在尝试我想做的事.
I'm looking for a way to combine jquery variables. Looking through related questions, nobody seems to be trying what I want to do.
$("body, div, p")
是通过选择器组合的一种方法,但是在将选择器分配给变量之后,如何以类似的方式组合它们?
$("body, div, p")
is one method of combining by selectors, but after you assign a selector to a variable, how do you combine them in a similar fashion?
这是我尝试过的一种方法,但是没有使它起作用.我还尝试将它们放入数组中(只需在$ body之前和$ p之后添加方括号).
Here is one method I tried, but did not get it to work. I also tried putting them into an array (by simply adding brackets before $body and after $p).
$body = $("body");
$div = $("div");
$p = $("p");
$mixed = $($body, $div, $p);
// $mixed = $("body, div, p"); is NOT what I am looking for
在我的实际脚本中,我已经为变量分配了一些<select>
输入.我想将它们分组,所以说我有一个<select>
带有"FRUIT",另一个有"VEGETABLES",我可以将它们都放入一个名为"PRODUCE"的jquery变量中.然后,如果我需要对两者都执行任何操作,则可以使用Produce代替.
In my actual script I've got some <select>
inputs assigned to variables. I would like to put them into groups, so say I have a <select>
which has "FRUIT" and another one that has "VEGETABLES", I could put them both into a jquery variable called "PRODUCE". Then if I need to perform anything on both of the two, I use produce instead.
...不过,这可能只是做事的愚蠢方式.
...This might just be a stupid way to do things, though.
推荐答案
也许您正在寻找 .add( )?
$mixed = $body.add($div).add($p);
这篇关于将jquery变量合并到一个选择器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!