Javascript选择器中的实时和非实时收集之间有什么区别? [英] What's the difference between live and not live collection in Javascript selectors?

查看:42
本文介绍了Javascript选择器中的实时和非实时收集之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道实时收集与非实时收集之间的区别.

How can I know what is the difference between live and not live collection.

根据我的研究:

实时是:当DOM中的更改反映在集合中时.修改节点时,内容遭受更改.

A live is: when the changes in the DOM are reflected in the collection. The content suffers the change when a node is modified.

不存在是:当DOM中的任何更改不影响集合的内容时.

A Not Live is : when any change in the DOM does not affect the content of the collection.

document.getElementsByClassName()是HTMLCollection,并且处于活动状态.

document.getElementsByClassName() is an HTMLCollection, and is live.

document.getElementsByTagName()是HTMLCollection,并且是实时的.

document.getElementsByTagName() is an HTMLCollection, and is live.

document.getElementsByName()是一个NodeList并且处于活动状态.

document.getElementsByName() is a NodeList and is live.

document.querySelectorAll()是一个NodeList,并且不存在.

document.querySelectorAll() is a NodeList and is not live.

为什么document.querySelectorAll不活跃?

我知道:

HTMLCollection 仅包含Elements节点 NodeList 包含元素节点和文本节点.

HTMLCollection only contains Elements Nodes NodeList contains Element Nodes and Text Nodes.

推荐答案

这些

document.getElementsByClassName()
document.getElementsByTagName()
document.getElementsByName()

之所以活着,是因为他们是引擎维护的内部集合的观察者.维护不是严格要求的,但是很容易实现.

are live because they are observers of internal collections maintained by engines. That maintenance is not strictly required but is easy to achieve.

document.querySelectorAll() 

无效,因为每次请求时都会计算结果.实时收集的维护成本太高,因为在这种情况下,对DOM的每次修改(内容,属性,类)都将需要重新评估收集中的每个元素- O(N * M)任务,其中N是DOM中所有元素的数量(最坏的情况),M是活动的 querySelectorAll()集合的数量.

is not live because result gets computed each time you request it. Maintenance of live collection is too expensive as each modification (content, attributes, classes) of the DOM in this case will require re-evaluation of each element in the collection - O(N*M) task where N is the number of all elements in the DOM (worst case) and M number of active querySelectorAll() collections.

这篇关于Javascript选择器中的实时和非实时收集之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆