使用jQuery选择具有数据属性* not *的元素 [英] Select elements with data attribute *not* using jQuery

查看:76
本文介绍了使用jQuery选择具有数据属性* not *的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以获取包含数据的所有元素的列表 - * 属性不使用使用jQuery(并且希望无需迭代整个DOM树)?

Is there a simple way to get a list of all elements containing a data-* attribute without using jQuery (and hopefully without iterating the whole DOM-tree) ?

我可以使用jQuery找到很多答案,但我不能在我的项目中使用它,也不能使用普通的Javascript找到答案。

I can find a lot of answers using jQuery but I cannot use it in my project and neither can I find answers using plain Javascript.

例如,如果我有这个HTML:

For example if I have this HTML:

<input data-mydata="data1">
<input data-mydata="data2">
<div data-mydata="data3"></div>
<div data-mydata="data4"></div>
<input>
<div></div>

然后获取包含的所有元素的列表data-mydata

我试图实现的是从某些元素中获取数据,以便根据数据*以及数据值。

What I try to achieve is to get data from certain elements for storage based on the data-* as well as the data value.

我知道我可以在 getElementsByName name 属性c $ c>但由于元素的名称设置用于其他目的,因此无法在我的项目中使用。

I know I can use the name attribute with getElementsByName but that is not possible to use in my project either as the elements have names set for other purposes.

我相信 querySelectorAll 仅适用于CSS(?)。

And I believe the querySelectorAll only works with CSS (?).

推荐答案

就是这样:

var elements = document.querySelectorAll('[data-mydata]');

这篇关于使用jQuery选择具有数据属性* not *的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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