简体   繁体   English

如果我不需要结果列表中的CSS选择器的粒度,为什么我会想要在“实时”NodeList上使用静态NodeList / HTMLCollection?

[英]Why would I ever want a static NodeList/HTMLCollection over a “live” NodeList if I don't need the granularity of CSS Selectors in the resulting list?

I generally hear that because live NodeLists are "bad" (see this Zakas article) and that informed the decision for querySelectorAll to return a static HTMLCollection . 我通常会听到,因为实时NodeLists是“坏” (请参阅此Zakas文章),并告知querySelectorAll返回静态HTMLCollection的决定。 Why do people think live NodeLists are a bad thing? 为什么人们认为实时NodeLists是一件坏事? Code examples would probably help me understand this best. 代码示例可能有助于我理解这一点。

If, whenever I care to use the value of a cached collection of Nodes for any computation that collection happens to not be a stale snapshot, I can't really see that as a "bad" thing. 如果,每当我关心使用缓存的节点集合的值进行任何计算时,集合恰好不是一个陈旧的快照,我就不能真正看到它是一个“坏”的东西。

I understand exactly how much more useful it is to select elements with a CSS Selector string, but if I can only reliably run code against that collection right after acquiring it, it seems to be quite a bit less useful than a live NodeList . 我完全理解用CSS Selector字符串选择元素有多么有用,但是如果我只能在获取它后立即可靠地运行代码,那么它似乎比实时NodeList

Live nodelists are not bad, but their behaviour can be confusing if you're not used to it. 实时节点列表也不错,但如果您不习惯它们,它们的行为可能会令人困惑。 Especially if you think of them as arrays (they're not arrays) 特别是如果你认为它们是数组(它们不是数组)

Consider a classic example of doubling the number of divs in a page. 考虑一个将页面中div数量加倍的经典示例。 Here are three attempts at this: 以下是三次尝试:

// Example 1 (doesn't work)
for(var i = 0; i < document.querySelectorAll("div").length ; i++){
    document.body.appendChild(document.createElement("div"));
}

// Example 2 (works)
var divs = document.querySelectorAll("div");
for(var i = 0; i < divs.length ; i++){
    document.body.appendChild(document.createElement("div"));
}

// Example 3 (doesn't work)
var divs = document.getElementsByTagName("div");
for(var i = 0; i < divs.length; i++){
    document.body.appendChild(document.createElement("div"));
}

Example 1 is clearly an infinite loop. 示例1显然是无限循环。 Each iteration, it rechecks the number of divs in the page. 每次迭代,它都会重新检查页面中的div数。

Example 2 works as expected because the nodelist is already cached (of course it would be better to simply cache the length). 示例2按预期工作,因为节点列表已经被缓存(当然,简单地缓存长度会更好)。

Example 3 looks like example 2. Many people would expect it to work the same way, as the nodelist is cached. 示例3看起来像示例2.许多人都希望它以相同的方式工作,因为节点列表是缓存的。 But as the nodelist is live, it is actually another infinite loop. 但是由于节点列表是实时的,它实际上是另一个无限循环。 This catches some people out. 这抓住了一些人。

Also, if a function returns a static nodelist, you can requery the DOM each time you need it. 此外,如果函数返回静态节点列表,则可以在每次需要时重新查询DOM。 This is arguably simpler than converting your live list to static. 这可以说比将实时列表转换为静态更简单。

live NodeList are quicker to retrieve, so they are more performant 实时 NodeList可以更快地检索,因此它们的性能更高
static NodeList is less performant. 静态节点列表是高性能。

see the difference by eg. 通过例如看到差异 between querySelector (querySelectorAll) and getElementById querySelector (querySelectorAll)和getElementById

In the same conditions getElementsByTagName is better to use than querySelectorAll ... 在相同的条件下getElementsByTagNamequerySelectorAll更好用...

At least that I read from the Microsoft official training guide "Programming in HTML5 with JavaScript and CSS3"... 至少我从微软官方培训指南“使用JavaScript和CSS3编写HTML5”中读到了......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM