admin管理员组文章数量:1332881
we are all familiar with getElementByID, getElementsByClassName, and document.querySelector() methods. My question would be, as HTML5 allows us to use data-set now, can we query elements by their data-set? Thanks.
we are all familiar with getElementByID, getElementsByClassName, and document.querySelector() methods. My question would be, as HTML5 allows us to use data-set now, can we query elements by their data-set? Thanks.
Share Improve this question asked Apr 15, 2014 at 15:31 James WayneJames Wayne 1,91214 silver badges14 bronze badges2 Answers
Reset to default 7There's not a lot you can do but you can get the elements having a specific variable in dataset using :
var elements = document.querySelectorAll('[data-name]');
and you can get elements having a specific value using
var elements = document.querySelectorAll('[data-name="value"]');
@dystroy's answer is probably exactly what you need. Additionally, query all the data-set might not be supported by all browsers. Instead, I create a class name that is similar (or identical) to the data-set name and use that as the anchor to query the elements.
本文标签: htmlIs it possible to get and query element by dataset when using javascriptStack Overflow
版权声明:本文标题:html - Is it possible to get and query element by dataset when using javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742315932a2451813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论