迹忆客 专注技术分享

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

使用 JavaScript 按内容查找元素

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

按内容查找元素:

  1. 使用 document.querySelectorAll 方法按标签选择 DOM 元素。
  2. 使用 for...of 循环遍历集合。
  3. 在每次迭代中,检查元素的 textContent 是否与预期内容匹配。

这是文章中示例的 html。

index.html

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

  <body>
    <div>Hello 迹忆客(jiyik.com)</div>
    <div>One two three</div>
    <div>Apple banana kiwi</div>

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

这是相关的 JavaScript 代码。

index.js

const text = 'Hello 迹忆客(jiyik.com)';

const matches = [];

for (const div of document.querySelectorAll('div')) {
  if (div.textContent.includes(text)) {
    matches.push(div);
  }
}

console.log(matches); // 👉️ [div.box]

此代码示例查找内容包含特定字符串的所有 DOM 元素。 如果您只需要查找与条件匹配的单个 DOM 元素,请向下滚动到使用 find() 方法的示例。

我们使用 document.querySelectorAll 方法来获取一个 NodeList,其中包含所有具有 div 标签的 DOM 元素。

下一步是使用 for...of 循环遍历 NodeList

在每次迭代中,我们检查元素的 textContent 是否包含预期的文本。 如果有匹配,我们将元素推送到匹配数组中。

如果我们想在检查 DOM 元素的内容是否包含特定字符串时忽略大小写,请将两个字符串都转换为小写。

const text = 'HELLO 迹忆客(JIYIK.COM)';

const matches = [];

for (const div of document.querySelectorAll('div')) {
  if (div.textContent.toLowerCase().includes(text.toLowerCase())) {
    matches.push(div);
  }
}

console.log(matches); // 👉️ [div.box]

通过将元素的 textContent 和我们要查找的文本转换为小写,我们可以进行不区分大小写的检查。

因为 for...of 循环遍历 NodeList 中的所有元素,所以如果您只需要与条件匹配的第一个元素,这种方法将效率低下。

要查找其内容包含特定字符串的第一个 DOM 元素:

  1. 使用 document.querySelectorAll 方法按标签选择元素。
  2. 使用 Array.from 方法将结果转换为数组。
  3. 使用 find() 方法遍历数组。
  4. 在每次迭代中,检查元素的内容是否包含字符串。
const text = 'Hello 迹忆客(jiyik.com)';

const elements = Array.from(document.querySelectorAll('div'));

const match = elements.find(el => {
  return el.textContent.includes(text);
});

console.log(match); // 👉️ div.box

我们传递给 Array.find 方法的函数会为数组中的每个元素调用,直到它返回一个真值或耗尽数组中的元素。

如果回调函数返回真值,则 find() 方法短路并返回相应的数组元素。

如果条件从未满足,则 find 方法返回 undefined

如果要对元素是否包含特定字符串进行不区分大小写的检查,请将元素的 textContent 和要检查的字符串转换为小写。

const text = 'Hello 迹忆客(jiyik.com)';

const elements = Array.from(document.querySelectorAll('div'));

const match = elements.find(el => {
  return el.textContent.toLowerCase().includes(text.toLowerCase());
});

console.log(match); // 👉️ div.box

通过将我们要比较的两个字符串都转换为小写,我们可以进行不区分大小写的比较。

转载请发邮件至 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

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便