使用带有通配符的jquery查找元素

时间:2013-06-19 13:51:54

标签: jquery

我有一个场景,我需要使用特定的字符串找到一个元素

("id^='16'")

我的要求是它应该只获取id ='16k','16M'的值而不是像'16KK'或'16MK'这样的值

即。它应该只获取id,它在搜索字符串后只有一个字符

3 个答案:

答案 0 :(得分:2)

对返回的元素使用filter

$("id^='16'").filter(
function() {
        return this.id.match(/16[a-zA-Z]/);
    });

答案 1 :(得分:1)

将jquery属性选择器与过滤器一起使用:

//jQuery requires brackets for Attribute Selector
$("[id^='16']").filter(
function() {
    //Match if not followed by another character
    return this.id.match(/16[a-zA-Z](?![a-zA-Z])/g);
});

答案 2 :(得分:0)

您可以创建一个jQuery自定义过滤器:

$(document).filter(function() {
    return $(this).attr("id").match( /16[a-z]/i ); 
});