javascript:选择所有包含“data-”的元素属性(不含jQuery) [英] javascript: select all elements with "data-" attribute (without jQuery)

查看:576
本文介绍了javascript:选择所有包含“data-”的元素属性(不含jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不使用jQuery的情况下,选择所有具有特定数据的dom元素 - 属性(让我们说 data-foo )。元素可以是不同的标签元素。

Without using jQuery, what is the most efficient way to select all dom elements that have a certain data- attribute (let's say data-foo). The elements may be different tag elements.

<p data-foo="0"></p><br/><h6 data-foo="1"></h6>


推荐答案

您可以使用 querySelectorAll

document.querySelectorAll('[data-foo]');

这篇关于javascript:选择所有包含“data-”的元素属性(不含jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆