【问题标题】:how to get a data-attribute where has class active如何获得类活动的数据属性
【发布时间】:2022-06-22 18:40:37
【问题描述】:

我有一个像下面这样的多个锚标记

<a class="sort-type active " data-type="desc" data-field="relevance" href="#" title="" target="_self">relevance</a>
<a class="sort-type " data-type="desc" data-field="most-viewed" href="#" title="" target="_self">most viewer</a>
<a class="sort-type " data-type="desc" data-field="high-price" href="#" title="" target="_self">high-price</a>

我的问题是如何获取其中有活动类的数据字段

【问题讨论】:

标签: javascript html jquery


【解决方案1】:

纯 JS 解决方案将是:

// document.querySelector(".sort-type.active") - Gets the element with active class
// getAttribute('data-field') - Gets the attribute required from the html

document.querySelector(".sort-type.active").getAttribute('data-field')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多