迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 通过 data 属性获取所有元素

作者:迹忆客 最近更新:2022/12/13 浏览次数:

要通过数据属性获取所有 DOM 元素,请使用 querySelectorAll 方法,例如 document.querySelectorAll('[data-id]')querySelectorAll 方法返回一个 NodeList,其中包含与指定选择器匹配的元素。

这是此示例的 html 代码。

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
  </head>

  <body>
    <div data-id="box1">Box 1</div>
    <div data-id="box2">Box 2</div>

    <script src="index.js"></script>
  </body>
</html>

这是相关的 JavaScript 代码。

index.js

// ✅ Get all elements with `data-id` attribute
const elements1 = document.querySelectorAll('[data-id]');
console.log(elements1); // 👉️ [div, div]

// ✅ Get only DIV elements with `data-id` attribute
const elements2 = document.querySelectorAll('div[data-id]');
console.log(elements2); // 👉️ [div, div]

// ✅ Get only elements where data-id = box1
const elements3 = document.querySelectorAll(`[data-id="box1"]`);
console.log(elements3); // 👉️ [div]

我们将不同的选择器传递给 document.querySelectorAll 方法以获取包含特定 DOM 元素的 NodeList。

请注意querySelectorAll 方法返回一个 NodeList,而不是一个数组。 如果您需要将响应转换为数组,请将其传递给 Array.from 方法。

const arr = Array.from(document.querySelectorAll('[data-id]'));

第一个示例展示了如何获取所有设置了 data-id 属性的 DOM 元素。

const elements1 = document.querySelectorAll('[data-id]');

在第二个示例中,我们将范围缩小到只有设置了 data-id 属性的 div 元素。

const elements2 = document.querySelectorAll('div[data-id]');

如果 DOM 包含任何设置了 data-id 属性的 span 或 p 元素,它们将不会包含在 querySelectorAll 方法的返回值中。

第三个示例选择 data-id 属性设置为 box1 的元素。

const elements3 = document.querySelectorAll(`[data-id="box1"]`);

请注意data-id 属性必须准确设置为 box1。

要根据特定属性值的部分匹配来选择元素,请向下滚动到下一个示例。

通过数据属性的部分匹配获取所有 DOM 元素

要通过部分匹配数据属性来获取所有元素,请使用带有选择器的 querySelectorAll 方法,该选择器匹配值以特定字符串开头、结尾或包含特定字符串的数据属性。

// ✅ Get all where value of data-id starts with `bo`
const elements1 = document.querySelectorAll('[data-id^="bo"]');
console.log(elements1); // 👉️ [div, div]

// ✅ Get all where value of data-id ends with `ox1`
const elements2 = document.querySelectorAll('[data-id$="ox1"]');
console.log(elements2); // 👉️ [div]

// ✅ Get all where value of data-id contains with `box`
const elements3 = document.querySelectorAll('[data-id*="box"]');
console.log(elements3); // 👉️ [div, div]

第一个示例选择 data-id 属性值以 bo 开头的所有 DOM 元素。

const elements1 = document.querySelectorAll('[data-id^="bo"]');

我们可能熟悉插入符 ^ 符号,它在正则表达式中使用时具有相同的含义。

第二个示例选择 data-id 属性值以 ox1 结尾的所有 DOM 元素。

const elements2 = document.querySelectorAll('[data-id$="ox1"]');

第三个示例选择所有 DOM 元素,其中 data-id 属性的值包含字符串框。

const elements3 = document.querySelectorAll('[data-id*="box"]');

为了满足条件,字符串框可以位于 data-id 属性值的任何位置。

我们还可以在选择器前面加上您要匹配的特定类型的元素以缩小结果范围。

const elements1 = document.querySelectorAll(`div[data-id^="bo"]`);

该示例仅选择具有 data-id 属性集且属性值以 bo 开头的 div 元素。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

Pandas DataFrame 重置索引

发布时间:2024/04/24 浏览次数:1517 分类:Python

本教程介绍了如何在 Python 中使用 pandas.DataFrame.reset_index()重置索引。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便