javascript jQuery 查找所有以定义的字符串开头并以数字结尾的 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5878342/
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
jQuery find all id's that begin with a defined string and end in a number
提问by Chris Dutrow
I would like to do something like this:
我想做这样的事情:
jqueryElement.find('divIdWithIterator**').each(...);
where 'divIdWithIterator**'
matches all elements with ids that start with 'divIdWithIterator'
and end in a number, such as:
where'divIdWithIterator**'
匹配所有 id 以数字开头'divIdWithIterator'
和结尾的元素,例如:
divIdWithIterator1, divIdWithIterator2, divIdWithIterator26
divIdWithIterator1, divIdWithIterator2, divIdWithIterator26
What is a good way to do this in jQuery?
在 jQuery 中执行此操作的好方法是什么?
Thanks!
谢谢!
回答by Phil
Unfortunately, there is no regular expression selector.
不幸的是,没有正则表达式选择器。
You could use the attribute-starts-with selector, then filterthe results and check for numeric endings using search()
您可以使用attribute-starts-with 选择器,然后过滤结果并使用检查数字结尾search()
Eg
例如
var divs = jqueryElement.find('[id^="divIdWithIterator"]').filter(function(index){
return this.id.search(/\d$/) != -1;
});
回答by ariel
Following: http://api.jquery.com/attribute-starts-with-selector/
以下:http: //api.jquery.com/attribute-starts-with-selector/
To find the elements whose id begin with "divIdWithIterator":
要查找 id 以“divIdWithIterator”开头的元素:
$('input[id^="divIdWithIterator"]')
And then avoid to have other elements that begin with "divIdWithIterator" and you don't want to select
然后避免有其他以“divIdWithIterator”开头的元素并且您不想选择