html - Is it possible to get and query element by dataset when using javascript? - Stack Overflow

we are all familiar with getElementByID, getElementsByClassName, and document.querySelector() methods.

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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

There'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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742315933a4420835.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信