开发者

jQuery find all id's that begin with a defined string and end in a number

开发者 https://www.devze.com 2023-03-02 10:13 出处:网络
I would like to do something like this: jqueryElement.find(\'divIdWithIterator**\').each(...); where \'divIdWithIterator**\' matches all elements with ids that start with \'divIdWithIterator\' and

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:

divIdWithIterator1, divIdWithIterator2, divIdWithIterator26

What is a good way to do this in jQuery?

T开发者_如何转开发hanks!


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()

Eg

var divs = jqueryElement.find('[id^="divIdWithIterator"]').filter(function(index){
    return this.id.search(/\d$/) != -1;
});


Following: http://api.jquery.com/attribute-starts-with-selector/

To find the elements whose id begin with "divIdWithIterator":

$('input[id^="divIdWithIterator"]')

And then avoid to have other elements that begin with "divIdWithIterator" and you don't want to select

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号