jQuery 在jQuery选择器上使用正则表达式查找基于id的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3086554/
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
Find all elements based on ids using regex on jQuery selector
提问by Steven Cheng
I've got several elements with unique ids like so:
我有几个具有唯一 ID 的元素,如下所示:
<div id='item-1-top'></div>
<div id='item-2-top'></div>
<div id='item-3-top'></div>
I was hoping that the following would work using jQuery:
我希望以下内容可以使用 jQuery:
$("#item-[.]+-top").each(function() {
$(this).hide();
});
I do not have a good grasp of regular expressions and I would appreciate some input, as the above appears to be incorrect.
我没有很好地掌握正则表达式,我希望得到一些输入,因为上面的内容似乎不正确。
回答by Peter Boughton
Ifyou were doing this with regex, the expression would simply be:
如果您使用正则表达式执行此操作,则表达式将简单地为:
item-\d-top
Where the \d
indicates any single digit (0..9), and the other characters have no special meaning (so are treated as literals).
其中\d
表示任何单个数字 (0..9),其他字符没有特殊含义(因此被视为文字)。
However, jQuery doesn't currently have a regex filter (only things like start/end/contains/etc) - so you would have to create your own one (which is possible, but if you were considering that you should stop and consider what/why you're filtering and figure out if there's a better way first).
但是,jQuery 目前没有正则表达式过滤器(只有开始/结束/包含/等) - 所以你必须创建自己的过滤器(这是可能的,但如果你正在考虑你应该停下来考虑什么/为什么要过滤并首先弄清楚是否有更好的方法)。
Much simpler would be to create a class (as serg555suggests), since that's exactly how you're treating these items.
创建一个类(如serg555建议)要简单得多,因为这正是您处理这些项目的方式。
Or (if you can't change the markup to add the class) then use the existing filters, expanding on g.d.d.c's answer, I might do:
或者(如果您无法更改标记以添加类)然后使用现有过滤器,扩展gddc 的答案,我可能会这样做:
$('div[id^=item-][id$=-top]').hide()
(Since you may have multiple items ending with just 'top', either now or in future, so you need to be more specific to avoid unintentionally hiding other things.)
(因为您现在或将来可能有多个以“top”结尾的项目,因此您需要更加具体,以避免无意中隐藏其他内容。)
回答by Hari K T
If the id was something like news-top-1
, news-top-2
, news-top-3
, news-top-4
etc. then the selectors would have helped you.
如果ID是像news-top-1
,news-top-2
,news-top-3
,news-top-4
等则选择会帮助你。
http://api.jquery.com/attribute-starts-with-selector/
http://api.jquery.com/attribute-starts-with-selector/
$.each( $("input[name^='news-top-']"), function () {
alert( $(this).hide() );
});
回答by serg
I would assign some class to them like item
and then do a search by this class $(".item")
.
我会为他们分配一些课程item
,然后按这个课程进行搜索$(".item")
。
回答by vijaykumar
James Padolsey created a wonderful filterthat allows regex to be used for selection.
James Padolsey 创建了一个很棒的过滤器,允许使用正则表达式进行选择。
jQuery.expr[':'].regex = function(elem, index, match) {
var matchParams = match[3].split(','),
validLabels = /^(data|css):/,
attr = {
method: matchParams[0].match(validLabels) ?
matchParams[0].split(':')[0] : 'attr',
property: matchParams.shift().replace(validLabels,'')
},
regexFlags = 'ig',
regex = new RegExp(matchParams.join('').replace(/^s+|s+$/g,''), regexFlags);
return regex.test(jQuery(elem)[attr.method](attr.property));
}
Now you can use
现在你可以使用
$('div:regex(id,item-[0-9]-top)').hide()
回答by harshvchawla
Hit a similar problem and liked/upvoted serg's answer of creating class instead but then because I was doing multiple operations on such elements, Keyed Collectionswere more suitable.
遇到了类似的问题,并且喜欢/赞成 serg 的创建类的答案,但是因为我对这些元素进行了多次操作,所以Keyed Collections更合适。