作为jQuery选择器的字符串数组? [英] an array of strings as a jQuery selector?

查看:106
本文介绍了作为jQuery选择器的字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效jQuery选择器的字符串数组(即页面上元素的ID):

I have an array of strings that are valid jQuery selectors (i.e. IDs of elements on the page):

["#p1", "#p2", "#p3", "#p4", "#p5"]

我想在jQuery数组中选择带有这些ID的元素。这可能是基本的,但我在网上找不到任何东西。我可以有一个for循环,它创建一个字符串#p1,#p2,#p3,#p4,#p5然后可以作为单个传递给jQuery选择器,但不是另一种方式?有没有办法将一个字符串数组作为选择器传递?

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?

编辑:实际上,有一个已经有了答案

推荐答案

好吧,有'加入':

["#p1", "#p2", "#p3", "#p4", "#p5"].join(", ")

编辑 - 额外信息:

可以选择一个元素数组,问题在这里你没有元素,只是选择器字符串。无论你如何切片,你都必须执行像.getElementById这样的搜索或使用实际的jQuery选择。

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.

这篇关于作为jQuery选择器的字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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