【问题标题】:Find all elements with a specific attribute name in jquery在jquery中查找具有特定属性名称的所有元素
【发布时间】:2014-04-23 02:01:04
【问题描述】:

考虑以下截断的代码:

<div id="sample">
 <div es-value="true" />
 .....
 <span es-change="false" />
</div>

如何获取名称以“es-”开头的属性的所有元素?

【问题讨论】:

标签: jquery


【解决方案1】:

更新

var test = $("div").filter(function() {
    var attr = this.attributes;
    for (var i=0; i<attr.length; i++) {
        if (attr[i].name.substr(0, 3) === "es-") {
            return true;
        }
    }
    return false;
});

console.log(test);

DEMO

【讨论】:

    【解决方案2】:

    我没有测试它,但这应该可以工作:

    jQuery.expr[':'].contains = function (a, i, m) {
    var elementList = [];
        $.each(jQuery(a).attr(),function(index,attribute)
    {
    if(attribute.toUpperCase().indexOf(m[3].toUpperCase()) >= 0)
    {
    elementList.push(attribute);
    }
    
    });
    return elementList;
    };
    
    $.find('div:contains(''es-'')');
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2013-02-04
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多