jQuery选择#id,其中word为前缀,counter为后缀 [英] jQuery Select # id with word as prefix and counter as suffix

查看:145
本文介绍了jQuery选择#id,其中word为前缀,counter为后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法选择带有前缀my和后缀0-9的jQuery的所有id。
类似这些$(#my $ 1-4)还是可以循环?

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 ?

    <div id="my1"/>
    <div id="my2"/>
    <div id="my3"/>
    <div id="my4"/>
    <div id="my5"/>


推荐答案

第一个想法,似乎运作良好:

First thoughts, which seems to work well:

$('div[id^="my"]').filter(
    function(){
        return this.id.match(/\d+$/);
    });

JS Fiddle演示

以上选择所有 div 元素的 id 以值 my 开头,然后将返回的元素过滤为 id 的元素也以数字字符结尾。

The above selects all div elements whose id starts with the value my, and then filters the returned elements to those whose id also ends with numeric characters.

参考文献:

  • attribute-starts-with selector.
  • filter().
  • Regular Expressions, at Mozilla Developer Network.

这篇关于jQuery选择#id,其中word为前缀,counter为后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆