是否有标识符(id)的通配符选择器?
如果我有未知数量的标识符共享特定的命名方案,有没有办法使用 jQuery 一次性获取它们?
// These are the IDs I'd like to select
#instance1
#instance2
#instance3
#instance4
// What do I need to add or how do I need to modify this jQuery selector in order to select all the IDs above?
("#instanceWILDCARD").click(function(){}
If I have an unknown amount of identifiers sharing a specific naming-scheme, is there a way to grab them all at once using jQuery?
// These are the IDs I'd like to select
#instance1
#instance2
#instance3
#instance4
// What do I need to add or how do I need to modify this jQuery selector in order to select all the IDs above?
("#instanceWILDCARD").click(function(){}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
属性开头选择器 (
'^=
) 将适用于您的 ID,如下所示:但是,请考虑为您的元素提供一个公共类,例如(我自己笑了)
.instance
,并使用该选择器:The attribute starts-with selector (
'^=
) will work for your IDs, like this:However, consider giving your elements a common class, for instance (I crack myself up)
.instance
, and use that selector:如果您确实想要匹配类,而不是 id,则语法会稍微复杂一些,因为类可以有多个值。
If you really want to match classes, not ids, the syntax is a bit more involved, since class can have multiple values.
我很惊讶没有人提到创建自己的过滤器选择器(通过扩展 jQuery 的选择器功能)。在这里,我创建了一个名为“likeClass”和“likeId”的通配符选择器,它接受任何通配符字符串,并将查找所有匹配的元素(类似于正则表达式匹配)。
代码:
示例用法:
现在假设您有多个名称相似的 div 元素,例如 .content-1、.content-2、.content-n... 等,并且你想选择它们。现在是蛋糕!
或
哦,是的,还有一件事......您也可以链接它。 :)
享受!
I'm surprised no one has mentioned creating your own filter selector (by extending jQuery's Selector functionality). Here I've created a wildcard selectors I called "likeClass" and "likeId" that accepts any wildcard string and will find all elements that are a match (similar to Regex matching).
Code:
Example Usage:
Now let's say you had multiple div elements with similar names like .content-1, .content-2, .content-n... etc and you want to select them. Now it's cake!
or
Oh yeah, one more thing... you can chain it too. :)
Enjoy!
如果您有 jQuery,则不需要额外的 expr 或任何花哨的东西
如上所述: https://stackoverflow.com/a/2220874/2845401
No need for additional expr or anything fancy if you have jQuery
As noted: https://stackoverflow.com/a/2220874/2845401
这些是 ID,但您可以执行类似的操作:
虽然这有点昂贵 - 如果您可以指定 a) 元素的类型或 b) DOM 中的一般位置,则会有所帮助,例如:
[id ^='...']
选择器基本上意味着“查找 ID 以该字符串开头的元素,类似于id$=
(ID 以该字符串结尾)等。您可以在 此处 的 jQuery 文档页面上可以找到完整的列表。
Those are IDs, but you can do something similar to:
That's a bit expensive though - it helps if you can specify either a) the type of element or b) a general position in the DOM, such as:
The
[id^='...']
selector basically means "find an element whose ID starts with this string, similar toid$=
(ID ends with this string), etc.You can find a comprehensive list on the jQuery Docs page here.
为什么不直接将
class = "instance"
分配给所有这些并使用$('.instance')
选择它们呢?Why don't you just assign
class = "instance"
to all of them and select them using$('.instance')
?使用胡萝卜。
jsFiddle 示例
Use the carrot.
jsFiddle example
这是 id 通配符问题的唯一正确答案。
这将得到“双引号中的通配符。还有更多!”,以及“Wild 前面有更多 BS。双引号中的通配符”。
您应该使用与“[]”标签不同的引号。
This is the only correct answer to the id wildcard question.
This will get "Wild card in double quotes. And there's more!!!", and also "More BS in front of Wild. Wild card in double quotes".
You should use quote marks that are different than your '[]' tags.
我们可以这样做:
We can do it this way: