jQuery查找以定义的字符串开头并以数字结尾的所有id

时间:2022-11-29 19:27:24

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:

其中'divIdWithIterator **'匹配所有以'divIdWithIterator'开头并以数字结尾的ID的元素,例如:

divIdWithIterator1, divIdWithIterator2, divIdWithIterator26

divIdWithIterator1,divIdWithIterator2,divIdWithIterator26

What is a good way to do this in jQuery?

在jQuery中执行此操作的好方法是什么?

Thanks!

2 个解决方案

#1


5  

Unfortunately, there is no regular expression selector.

不幸的是,没有正则表达式选择器。

You could use the attribute-starts-with selector, then filter the 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;
});

#2


3  

Following: 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”开头并且您不想选择的其他元素

#1


5  

Unfortunately, there is no regular expression selector.

不幸的是,没有正则表达式选择器。

You could use the attribute-starts-with selector, then filter the 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;
});

#2


3  

Following: 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”开头并且您不想选择的其他元素