javascript - 为不在锚点内的所有图像添加事件侦听器
问题描述
如何在没有原生JavaScript 中的任何库(没有任何库)的情况下选择<img>
不在锚点内的所有元素?<a>
最精通性能的方法是什么?
解决方案
结果很简单:
// Use CSS-like selector
const images = document.querySelectorAll(":not(a) > img");
// Foreach it
images.forEach((img) => {
// Use the img variable
}
推荐阅读
- mysql - 导入两列并根据不同的分隔符和第一列将第二列分成多列和多行
- amazon-web-services - aws elasticsearch中长类型的范围查询
- javascript - DateFNSclosestIndexTo 返回 NaN
- javascript - 如何在 vuejs 中添加日期选择器(日期开始和结束日期)
- sql - 为什么 SQL Server 向我的 nocheck FK 约束添加检查约束?
- swift - 补充视图的节号是多少?
- sql - 在 1 个 sql 文件中执行多个删除表语句
- .net - 错误是说 dotnetcore 3.0 SDK 不支持 netstandard 2.1
- python - 如何使用多处理来解析和处理大型 XML 文件?
- continuous-integration - 在 Atlassian Bamboo 中,有没有办法将构建结果导出到 Excel 工作表或至少导出为 CSV?