[英]how to get a data-attribute where has class active
我有一個像下面這樣的多個錨標簽
<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>
我的問題是我怎樣才能獲得其中有類活動的數據字段
純 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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.