如何通过匹配部分属性来选择带有jQuery的元素? [英] How can I select an element with jQuery by matching a partial attribute?

查看:60
本文介绍了如何通过匹配部分属性来选择带有jQuery的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我希望匹配所有属性中包含data-foo的元素:

For example, I want to match all elements that have "data-foo" within any of their attributes:

<a data-foo="abc" href="#">I'm Matched</a>
<a data-foo-bar="abc" href="#">I'm Matched Too</a>
<a data-foo-bar-baz="abc" href="#">I'm Matched Too</a>

<a data-bar-foo="abc" href="#">I'm Not Matched</a>

我可以轻松遍历所有元素并检查自己,但我不确定是否支持jQuery这种类型的选择器。

I could easily loop through all of the elements and check myself but I wasn't sure if jQuery supported this type of selector.

推荐答案

检查这个小提琴...那里有点但也许你可以解决这个问题

check this fiddle...a little out there but maybe you could work off this

JSFiddle

$("a").each(function(){
    var element = $(this);
    var isItMe = 0;
        $(element[0].attributes).each(function() {
             if(this.nodeName.indexOf("data-bar") >= 0){
                isItMe = 1;
             }
        });
    if(isItMe == 1){
        $(this).css({"color" : "red"});  
    }
});

这篇关于如何通过匹配部分属性来选择带有jQuery的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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