Select elements with id attribute that starts/ends with a particular value

后端 未结 7 1279
予麋鹿
予麋鹿 2020-12-05 17:55

Can jQuery or JavaScript be used with a regex to select multiple elements with similar id?

I have the following paragraphs:

相关标签:
7条回答
  • 2020-12-05 18:21

    Yes it can, you can combine the attribute starts with and the attribute ends with selector

    $('[id^="item"][id$="2"]').html("D");
    

    FIDDLE (and you have to enable jQuery in the fiddle)

    you can't use regex to match the numbers in between though, for that you'd need filter()

    $('[id^="item"]').filter(function() {
        return this.id.match(/item\d+_2/);
    }).html("D");
    
    0 讨论(0)
提交回复
热议问题