I have an array of strings that are valid jQuery selectors (i.e. IDs of elements on the page):
我有一个字符串数组,它们是有效的jQuery选择器(即页面上元素的id):
["#p1", "#p2", "#p3", "#p4", "#p5"]
I want to select elements with those IDs into a jQuery array. This is probably elementary, but I can't find anything online. I could have a for-loop which creates a string "#p1,#p2,#p3,#p4,#p5"
which could then be passed to jQuery as a single selector, but isn't there another way? Isn't there a way to pass an array of strings as a selector?
我想在jQuery数组中选择具有这些id的元素。这可能是最基本的,但我在网上找不到任何东西。我可以有一个for循环,它创建一个字符串“#p1,#p2,#p3,#p4,#p5”,然后可以作为一个选择器传递给jQuery,但不是有其他的方式吗?难道没有办法将字符串数组作为选择器传递吗?
EDIT: Actually, there is an answer out there already.
编辑:事实上,已经有答案了。
7 个解决方案
#1
43
Well, there's 'join':
有“加入”:
["#p1", "#p2", "#p3", "#p4", "#p5"].join(", ")
EDIT - Extra info:
编辑-额外的信息:
It is possible to select an array of elements, problem is here you don't have the elements yet, just the selector strings. Any way you slice it you're gonna have to execute a search like .getElementById or use an actual jQuery select.
可以选择一个元素数组,问题是这里还没有元素,只有选择器字符串。无论如何,你都要执行一个搜索,比如。getelementbyid或者使用一个实际的jQuery select。
#2
12
Try the Array.join method:
的数组。连接方法:
var a = ["#p1", "#p2", "#p3", "#p4", "#p5"];
var s = a.join(", ");
//s should now be "#p1, #p2, #p3, ..."
$(s).whateverYouWant();
#3
6
What about $(foo.join(", "))
?
美元(foo。加入(","))?
#4
4
Use the array.join method to join them together
使用数组。连接方法将它们连接在一起
$(theArray.join(','));
#5
2
I think you're looking for join.
我想你想加入。
var arr = ["#p1", "#p2", "#p3", "#p4", "#p5"];
$(arr.join(","))
#6
2
If you want them to do something individually there is also .each();
如果你想让他们单独做某件事,还有。each();
In the example below, each p ids clicks makes any one of them red:
在下面的例子中,每个p id的点击都会使其中的任何一个变为红色:
var peas = ['#p1','#p2','#p3','#p4','#p5'];
$.each(peas, function(i){
$(peas[i]).click( function(){
$(peas[i]).css({'color':'red'});
});
});
When you throw 'i' into a function parameter, it finds the values inside the arrays appropriately. When you do '.each()' the format looks like this:
当您将“i”放入函数参数时,它会适当地找到数组中的值。当你这样做时,格式是这样的:
$.each(array, function(i){
// any code you wish as long as you have an array selector
//$(array[i]).whatever function
});
An even bigger example. Say you want to make the P you click on red, but want the other ps return to default color. Just make an array of nonPea and voila!
一个更大的例子。如果你想让你点击的P是红色的,但是想让其他的ps返回到默认的颜色。只需要做一组nonPea和瞧!
var peas = ['#p1','#p2','#p3','#p4','#p5']
, nonPeas = ['#p5, #p2, #p3, #p4'
,'#p1, #p5, #p3, #p4'
,'#p1, #p2, #p5, #p4'
,'#p1, #p2, #p3, #p5'
,'#p1, #p2, #p3, #p4']
;
$.each(peas, function(i){
$(peas[i]).click( function(){
$(peas[i]).css({'color':'red'});
$(nonPeas[i]).css({'color':'black'});
});
});
I know someone is bound to want to know about each value array as jquery selectors. Hope everything goes well!
我知道一定有人想知道每个值数组作为jquery选择器。希望一切顺利!
Source: jQuery .each()
来源:jQuery . each()
#7
0
Shorter:
短:
["#p1", "#p2", "#p3", "#p4", "#p5"].toArray()
#1
43
Well, there's 'join':
有“加入”:
["#p1", "#p2", "#p3", "#p4", "#p5"].join(", ")
EDIT - Extra info:
编辑-额外的信息:
It is possible to select an array of elements, problem is here you don't have the elements yet, just the selector strings. Any way you slice it you're gonna have to execute a search like .getElementById or use an actual jQuery select.
可以选择一个元素数组,问题是这里还没有元素,只有选择器字符串。无论如何,你都要执行一个搜索,比如。getelementbyid或者使用一个实际的jQuery select。
#2
12
Try the Array.join method:
的数组。连接方法:
var a = ["#p1", "#p2", "#p3", "#p4", "#p5"];
var s = a.join(", ");
//s should now be "#p1, #p2, #p3, ..."
$(s).whateverYouWant();
#3
6
What about $(foo.join(", "))
?
美元(foo。加入(","))?
#4
4
Use the array.join method to join them together
使用数组。连接方法将它们连接在一起
$(theArray.join(','));
#5
2
I think you're looking for join.
我想你想加入。
var arr = ["#p1", "#p2", "#p3", "#p4", "#p5"];
$(arr.join(","))
#6
2
If you want them to do something individually there is also .each();
如果你想让他们单独做某件事,还有。each();
In the example below, each p ids clicks makes any one of them red:
在下面的例子中,每个p id的点击都会使其中的任何一个变为红色:
var peas = ['#p1','#p2','#p3','#p4','#p5'];
$.each(peas, function(i){
$(peas[i]).click( function(){
$(peas[i]).css({'color':'red'});
});
});
When you throw 'i' into a function parameter, it finds the values inside the arrays appropriately. When you do '.each()' the format looks like this:
当您将“i”放入函数参数时,它会适当地找到数组中的值。当你这样做时,格式是这样的:
$.each(array, function(i){
// any code you wish as long as you have an array selector
//$(array[i]).whatever function
});
An even bigger example. Say you want to make the P you click on red, but want the other ps return to default color. Just make an array of nonPea and voila!
一个更大的例子。如果你想让你点击的P是红色的,但是想让其他的ps返回到默认的颜色。只需要做一组nonPea和瞧!
var peas = ['#p1','#p2','#p3','#p4','#p5']
, nonPeas = ['#p5, #p2, #p3, #p4'
,'#p1, #p5, #p3, #p4'
,'#p1, #p2, #p5, #p4'
,'#p1, #p2, #p3, #p5'
,'#p1, #p2, #p3, #p4']
;
$.each(peas, function(i){
$(peas[i]).click( function(){
$(peas[i]).css({'color':'red'});
$(nonPeas[i]).css({'color':'black'});
});
});
I know someone is bound to want to know about each value array as jquery selectors. Hope everything goes well!
我知道一定有人想知道每个值数组作为jquery选择器。希望一切顺利!
Source: jQuery .each()
来源:jQuery . each()
#7
0
Shorter:
短:
["#p1", "#p2", "#p3", "#p4", "#p5"].toArray()