【问题标题】:Selector placehoder for a single charcter单个字符的选择器占位符
【发布时间】:2013-12-29 23:59:39
【问题描述】:

我正在使用 jQuery 选择器,我需要根据字符的位置选择一些元素。例如,我需要选择那些第 3 个字符为“h”,第 4 个字符为“r”的项目。从下面的列表中,选择器应该选择前两个元素,而不是第三个。

abhres

cdhrex

efdghr

【问题讨论】:

    标签: jquery html css regex jquery-selectors


    【解决方案1】:

    您将不得不编写一个自定义选择器http://jquery-howto.blogspot.com/2009/06/jquery-custom-selectors-with-parameters.html Bellow 是我认为可以满足您的需求的一个示例。如果不是,那么修改它以获得您想要的东西应该不难

    $.expr[':']['nth-letter-eq'] = function (obj, index, meta, stack) {{
       var args = meta[3].split(",");
       var includeThisElement = false;
       if ($(obj).text().charAt(parseInt(args[0])) == args[1]) {
           includeThisElement = true;
       }
       return includeThisElement;
    }
    

    用法

    $("p:nth-letter-eq(3,a)")
    

    【讨论】:

      【解决方案2】:

      在此处查看第三个字符为 h

      http://regexr.com?37k0n

      您可以将第 4 个字符相应地更改为 r

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-22
        • 2012-01-12
        • 1970-01-01
        • 1970-01-01
        • 2022-06-30
        • 1970-01-01
        相关资源
        最近更新 更多