jQuery 标识符 (id) 是否有通配符选择器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3697542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a wildcard selector for identifiers (id)?
提问by Matt Elhotiby
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?
如果我有未知数量的标识符共享特定的命名方案,有没有办法使用 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(){}
回答by Nick Craver
The attribute starts-with selector ('^=
)will work for your IDs, like this:
该属性开始,与选择('^=
)会为你的ID的工作,就像这样:
$("[id^=instance]").click(function() {
//do stuff
});
However, consider giving your elements a common class, for instance (I crack myself up) .instance
, and use that selector:
但是,请考虑为您的元素提供一个通用类,例如(我自己搞砸了).instance
,并使用该选择器:
$(".instance").click(function() {
//do stuff
});
回答by Eric
If you really want to match classes, not ids, the syntax is a bit more involved, since class can have multiple values.
如果你真的想匹配类,而不是 id,语法会更复杂一些,因为类可以有多个值。
// handle elements like <div class="someclass1"></div>
$('[class^="someclass"]').click(function() {
// do stuff
});
// handle elements like <div class="foo someclass1"></div>
$('[class*=" someclass"]').click(function() {
// do stuff
});
回答by Timothy Perez
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).
我很惊讶没有人提到创建自己的过滤器选择器(通过扩展 jQuery 的选择器功能)。在这里,我创建了一个通配符选择器,我称之为“likeClass”和“likeId”,它接受任何通配符字符串,并会找到所有匹配的元素(类似于正则表达式匹配)。
Code:
代码:
$.expr[':'].likeClass = function(match){
return $('[class*=" '+ match +'"]');
};
$.expr[':'].likeId = function(match){
return $('[id*=" '+ match +'"]');
};
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!
现在假设您有多个名称相似的 div 元素,例如 .content-1、.content-2、.content-n... 等,并且您想要选择它们。现在是蛋糕!
$('div:likeClass(content-)'); // Returns all elements that have a similar Classname: content-*
$('div:likeClass(content-)'); // 返回所有具有相似类名的元素: content-*
or
或者
$('div:likeClass(content-)'); // Returns all elements that have a similar ID: content-*
$('div:likeClass(content-)'); // 返回所有具有相似 ID 的元素: content-*
Oh yeah, one more thing... you can chain it too. :)
哦,是的,还有一件事......你也可以链接它。:)
$('li:likeId(slider-content-)').hide().addClass('sliderBlock').first().fadeIn('fast');
Enjoy!
享受!
回答by Shinrai
No need for additional expr or anything fancy if you have jQuery
如果你有 jQuery,则不需要额外的 expr 或任何花哨的东西
jQuery('[class*="someclass"]').click(function(){
});
jQuery('[id*="someclass"]').click(function(){
});
回答by casablanca
Why don't you just assign class = "instance"
to all of them and select them using $('.instance')
?
为什么不分配class = "instance"
给所有这些并使用 选择它们$('.instance')
?
回答by mway
Those are IDs, but you can do something similar to:
这些是 ID,但您可以执行以下操作:
$("[id^='instance']").click(...)
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:
不过这有点贵 - 如果您可以指定 a) 元素的类型或 b) 在 DOM 中的一般位置,它会有所帮助,例如:
$("#someContentDiv span[id^='instance']").click(...)
The [id^='...']
selector basically means "find an element whose ID starts with this string, similar to id$=
(ID ends with this string), etc.
所述[id^='...']
选择器基本上意味着“找到一个元素的ID开始以该字符串,类似于id$=
(以该字符串ID端)等。
You can find a comprehensive list on the jQuery Docs page here.
您可以在此处的 jQuery 文档页面上找到完整列表。
回答by jhanifen
回答by Thomas
We can do it this way:
我们可以这样做:
$(document).ready(function () {
$('[id*=btnOk]').live("click", function () {
});
});
回答by Kurt
This is the only correct answer to the id wildcard question.
这是 id 通配符问题的唯一正确答案。
$('[id*="Wild card in double quotes"]')
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".
这将得到“双引号中的通配符。还有更多!!!”,以及“Wild 前面的更多 BS。双引号中的通配符”。
You should use quote marks that are different than your '[]' tags.
您应该使用与“[]”标签不同的引号。