开发者

jquery regexp selector

I have a simple example:

<li id="item0" class="test"> 
    ....开发者_运维问答.
<li id="item10" class="test"> 

How to remove class "test" from all elements this id =~ "#item\d{1,}" using jquery ?


Use the attribute starts with selector to get just those IDs:

$('li[id^="item"]').removeClass('test');


$(youritem).removeClass('test');


I believe you would need a ^= selector on the element.

$('li[id^=item]').removeClass('test');

Check the jsfiddle


If you want to use regex expressions in your jQuery selectors, check out this plugin: http://james.padolsey.com/javascript/regex-selector-for-jquery/

In this context it would be something like:

$('.test:regex(id, ^item([0-9]+)$)').removeClass('test');

See jsFiddle for demo

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜