js在nodeList中查找对象?

编程入门 行业动态 更新时间:2024-10-19 05:26:56
本文介绍了js在nodeList中查找对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

回退无关紧要.请不要图书馆.

Fallback is irrelevant. No libraries, please.

我们有一个 dom 对象引用,我们将调用 obj.它实际上是一个 event.target.

We have an dom object reference, we'll call obj. It's actually an event.target.

我们有一个节点列表,我们将调用 nodes,这是我们通过 querySelectorAll 和一个变量选择器获得的.

We have a node list, we'll call nodes, which we've gotten with querySelectorAll and a variable selector.

nodes 可能有 1 个或多个元素,每个元素都可能有子元素.

nodes may have 1 or many elements, and each each of those elements may have children.

我们需要确定 obj 是否是这些节点元素之一,或者这些节点元素的子元素.我们在这里寻找原生"浏览器功能,我们可以编写我们自己的 for 循环并实现这一点,我们正在寻找替代方案.

We need to determine if obj is one of those node elements, or children elements of those node elements. We're looking for "native" browser functionality here, we can totes write our own for loop and accomplish this, we are looking for alternatives.

类似于:

nodes.contains(obj) OR nodes.indexof(obj)

涉及检索要匹配的节点列表的其他方法的解决方案是可以接受的,但我不知道那些可能是什么.

Solutions involving other methods of retrieving the node list to match against are acceptable, but I have no idea what those could be.

推荐答案

我认为没有内置的 DOM 方法.您需要递归遍历您的 NodeList,并检查与您的元素是否相等.另一种选择是使用 Element.querySelectorAll 在 NodeList 中的每个第一级元素上(例如,查找元素的 id).不过,我不确定那会有多高的(旅馆)效率.

I don't think there's a built-in DOM method for that. You'd need to recursively traverse your NodeList, and check for equality with your element. Another option is to use Element.querySelectorAll on each first-level elements from your NodeList (looking for your element's id, for example). I'm not sure how (inn)efficient that would be, though.

更多推荐

js在nodeList中查找对象?

本文发布于:2023-11-07 06:05:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   js   nodeList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!