【问题标题】:find element by it's attribute value [duplicate]通过它的属性值查找元素[重复]
【发布时间】:2015-11-27 14:32:30
【问题描述】:
$('a').filter(function () {
    return $(this).attr('area-expanded')
});

将返回一个链接数组,其属性名为'area-expanded'

我需要找到一个链接,其属性'area-expanded'的值为true(页面上总是有一个这样的链接)。

我尝试了以下方法:

$('a').filter(function(){return $(this).attr('area-expanded').val() == true});

但我得到了

Uncaught TypeError: Cannot read property 'val' of undefined

我完全是 JS 的菜鸟,所以我确信我缺少一些类似的东西。 谢谢!

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    使用attribute-value 选择器:

    选择具有指定属性且值与某个值完全相等的元素。

    $('a[area-expanded="true"]')
    

    这将选择所有<a>,其aria-expanded 属性值设置为true

    【讨论】:

      猜你喜欢
      • 2018-09-26
      • 2012-06-30
      • 2014-03-12
      • 2012-07-08
      • 1970-01-01
      • 2020-01-01
      • 2018-10-25
      • 2022-07-08
      相关资源
      最近更新 更多