如何按属性名称的一部分选择元素

问题描述 投票:0回答:1

HTML

<div data-a-b-c="...">A</div>
<div data-a-b-d="...">B</div>
<div data-a-c-b="...">C</div>

我想通过使用类似的东西选择节点AB

document.querySelectorAll("[a-b*]")

过去在Chrome中工作但随着时间的推移而破碎。我只需要支持现代浏览器。

html5 dom selector
1个回答
1
投票

Array.prototype.slice.call(document.querySelectorAll("*"))
  .filter(element => 
    Array.prototype.slice.call(
      element.attributes
    )
    .find(att => 
      att.localName.match(/data-/)
    )
  )
© www.soinside.com 2019 - 2024. All rights reserved.