javascript js在nodeList中找到对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12980527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
js find object in nodeList?
提问by Randy Hall
Fallback is irrelevant. No libraries, please.
回退无关紧要。请不要图书馆。
We have an dom object reference, we'll call obj
. It's actually an event.target.
我们有一个 dom 对象引用,我们将调用obj
. 它实际上是一个 event.target。
We have a node list, we'll call nodes
, which we've gotten with querySelectorAll and a variable selector.
我们有一个节点列表,我们将调用它nodes
,这是我们通过 querySelectorAll 和一个变量选择器获得的。
nodes
may have 1 or many elements, and each each of those elements may have children.
nodes
可能有 1 个或多个元素,每个元素都可能有子元素。
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.
我们需要确定obj
是这些节点元素之一,还是这些节点元素的子元素。我们在这里寻找“本机”浏览器功能,我们可以编写自己的for
循环并实现这一点,我们正在寻找替代方案。
Something like:
就像是:
nodes.contains(obj)
OR nodes.indexof(obj)
nodes.contains(obj)
或者 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.
涉及检索节点列表以匹配的其他方法的解决方案是可以接受的,但我不知道那些可能是什么。
采纳答案by bfavaretto
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.
我不认为有一个内置的 DOM 方法。您需要递归遍历您的NodeList
,并检查与您的元素是否相等。另一种选择是Element.querySelectorAll
在您的每个第一级元素上使用NodeList
(例如,查找元素的 id)。不过,我不确定(旅馆)的效率如何。
回答by KthProg
I'm not sure if this will search beyond the first level of the NodeList, but you can use this expression recursively to traverse it and check if the element 'obj' is in the NodeList 'nodes'.
我不确定这是否会超出 NodeList 的第一级进行搜索,但是您可以递归地使用此表达式来遍历它并检查元素“obj”是否在 NodeList 的“节点”中。
[].indexOf.call(nodes, obj)
回答by Matt Goo
I did something like this:
我做了这样的事情:
Array.prototype.find.call(style.childNodes, function(child) {
if(child.textContent.includes(drawer.id)) {
console.log(child);
}
});
Seems to work. Then child is another html node, which you can manipulate however you like.
似乎工作。然后 child 是另一个 html 节点,您可以随意操作它。
回答by Dominic
If <=IE11 is not a concern then I think the cleanest is to use Array.from
如果 <=IE11 不是问题,那么我认为最干净的方法是使用Array.from
Array.from(nodes).find(node => node === nodeRefToFind);
I'm not entirely sure you can depend on node references like that but you can search for some other property on the node too (e.g. id, class or other attribute).
我不完全确定您是否可以依赖这样的节点引用,但您也可以在节点上搜索其他一些属性(例如 id、class 或其他属性)。