jQuery Select # id with word as prefix and counter as suffix

后端 未结 3 2217
南旧
南旧 2021-02-20 14:56

Is there a way to select all id\'s with jQuery with a prefix \"my\" and a suffix \"0-9\". Something like these $(\"#my$1-4\") or is it just possible with a loop ?



        
3条回答
  •  不思量自难忘°
    2021-02-20 15:16

    The prefix part is easily achievable with an attribute starts-with selector:

    $("div[id^=my]");
    

    But there is no selector that will allow you to specify a range of characters, so a loop will have to be involved. I would suggest filter:

    $("div").filter(function () {
        return /^my\d$/.test(this.id);
    });
    

提交回复
热议问题