最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery select anchors whose custom attr is not undefined - Stack Overflow

programmeradmin2浏览0评论
<a myattr="something">anchor</a>
<a>anchor</a>

$(a[myattr]) does not work
$(a[myattr!="") does not work
$(a[myattr!==undefined) does not work

What works for getting those anchors whose "myattr" are defined and equal to something?

<a myattr="something">anchor</a>
<a>anchor</a>

$(a[myattr]) does not work
$(a[myattr!="") does not work
$(a[myattr!==undefined) does not work

What works for getting those anchors whose "myattr" are defined and equal to something?

Share Improve this question asked Sep 10, 2012 at 15:57 Pablo CollaPablo Colla 2171 silver badge9 bronze badges 6
  • 6 If you put the selectors between quotes, your first one should work. jsfiddle/ult_bo/nuUrt – Fabrício Matté Commented Sep 10, 2012 at 15:59
  • @FabrícioMatté You should post that as an answer.. – Selvakumar Arumugam Commented Sep 10, 2012 at 16:01
  • Note that, as far as I remember, void attribute selectors don't work in IE7- (maybe even buggy in IE8 in special binations) – Seimen Commented Sep 10, 2012 at 16:01
  • $(a[myattr]) should be like $('a[myattr]') .. Missing the quotes for the selector expression.. – Sushanth -- Commented Sep 10, 2012 at 16:01
  • I thought OP could have a more plex problem and just typo'd it, I'm pretty sure the attribute selectors should work even in older IEs as jQuery utilizes Sizzle. – Fabrício Matté Commented Sep 10, 2012 at 16:03
 |  Show 1 more ment

3 Answers 3

Reset to default 6

If you put the selector between quotes, your first one should work.

$('a[myattr]')

Fiddle

http://jsfiddle/Be9Ab/

$("a[myattr]").css("color","red");​

Works just fine.

    jQuery(function(){
        jQuery("a").each(function(){
            if(jQuery(this).attr("myattr") && jQuery(this).attr("myattr")!==""){
                jQuery(this).addClass("myattr-link").css("color", "red");
            }
        });
    });
发布评论

评论列表(0)

  1. 暂无评论