- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
DOM4 使 NodeList可迭代:
interface <i>NodeList</i> {
getter <a href="https://www.w3.org/TR/dom/#node" rel="noreferrer noopener nofollow">Node</a>? <a href="https://www.w3.org/TR/dom/#dom-nodelist-item" rel="noreferrer noopener nofollow">item</a>(unsigned long <i>index</i>);
readonly attribute unsigned long <a href="https://www.w3.org/TR/dom/#dom-nodelist-length" rel="noreferrer noopener nofollow">length</a>;
iterable<<a href="https://www.w3.org/TR/dom/#node" rel="noreferrer noopener nofollow">Node</a>>;
};
根据 WebIDL , 这意味着
Objects implementing an interface that is declared to be iterable support being iterated over to obtain a sequence of values.
Note: In the ECMAScript language binding, an interface that is iterable will have “entries”, “forEach”, “keys”, “values” and @@iterator properties on its interface prototype object.
所以以下是可能的:
for (var el of document.querySelectorAll(selector)) ...
我注意到在 Firefox 和 Chrome 上,HTMLCollections 似乎同样有效:
for (var el of document.getElementsByTagName(tag)) ...
其实我明白了
HTMLCollection.prototype[Symbol.iterator] === [][Symbol.iterator]; // true
然而,HTMLCollection未定义为可迭代的:
interface <i>HTMLCollection</i> {
readonly attribute unsigned long <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-length" rel="noreferrer noopener nofollow">length</a>;
getter <a href="https://www.w3.org/TR/dom/#element" rel="noreferrer noopener nofollow">Element</a>? <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-item" rel="noreferrer noopener nofollow">item</a>(unsigned long <i>index</i>);
getter <a href="https://www.w3.org/TR/dom/#element" rel="noreferrer noopener nofollow">Element</a>? <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-nameditem" rel="noreferrer noopener nofollow">namedItem</a>(DOMString <i>name</i>);
};
我还检查了 WHATWG DOM spec它也不是可迭代的。
那么,这种行为规范不规范呢? HTMLCollection
应该在原型(prototype)中有一个 @@iterator
吗?
最佳答案
我找到了,在WebIDL中有解释:
If the interface has any of the following:
- an iterable declaration
- an indexed property getter and an integer-typed attribute named “length”
- a maplike declaration
- a setlike declaration
then a property must exist whose name is the @@iterator symbol, with attributes { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true } and whose value is a function object. [...]
If the interface defines an indexed property getter, then the Function object is %ArrayProto_values%.
在这种情况下,HTMLCollections 有一个索引属性 getter:
getter <a href="https://www.w3.org/TR/dom/#element" rel="noreferrer noopener nofollow">Element</a>? <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-item" rel="noreferrer noopener nofollow">item</a>(unsigned long <i>index</i>);
和一个名为“length”的整数类型属性:
readonly attribute unsigned long <a href="https://www.w3.org/TR/dom/#dom-htmlcollection-length" rel="noreferrer noopener nofollow">length</a>;
因此,是的,它应该有效。事实上,它也适用于 NodeLists,即使它们没有被声明为可迭代的,但它们不会有 entries
、forEach
、keys
和 values
属性。正如@lonesomeday 提到的,很可能 HTMLCollection 没有定义为可迭代的,因为添加这些方法不会向后兼容,因为 namedItem
getter 接受任意字符串。
关于javascript - HTMLCollections 可以用 for...of (Symbol.iterator) 迭代吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41758982/
有点好奇,因为我知道 HTMLCollections 会自动更新自己,所以我尝试使用 Proxy 观察它们。如下所示 let imagesCollection = document.images; l
// Find all posts var posts = node.getElementsByClassName("userContentWrapper"); var post, text; con
如果我调用document.getElementsByClassName('cl'),我会得到一个 HTMLCollection。它似乎包含 Element 对象而不是 HTMLElement 对象,
rowGetter = i => { const row = this.state.estimateItemList[i]; const selectRevison = this.st
我有一个页面加载由 1000 个元素组成的 HTMLCollection。 我想要实现的是,一旦页面加载,我希望 HTMLCollection 立即被打乱。 (我无法控制如何从服务器接收数据) 我不知
我知道有关于如何访问和迭代 HtmlCollection 的答案,但它在这里对我不起作用: 我得到了一些类为“tabSheetActive”的元素,这些元素的数量可以是 1 或更多。 我通过以下方式访
当我使用 getElementsByClassName 时,我无法访问 HTMLCollection 类型。 我想得到长度但是我不能得到那个 var documentHeader = parent.d
我做了一个网站,需要插入 javascript。我想从我的 HTML 菜单中获取子节点。 这是我的 HTML 结构的一部分:
我发现 HTMLOptionsCollection 对象继承了 HTMLCollection 的属性和方法。 HTMLOptionsCollections->HTMLCollection->Objec
我在这方面遇到了一些困难。在 Backbone 中,我有一个这样的函数: functionOne: function(){ $('#myTExtbox-' + budgetLine.
我有静态 html 存储在 javascript 字符串文字模板中。 单击按钮时(模式弹出窗口),我需要通过函数传递存储的静态 html,并使用存储在隐藏 div 中的动态数据更新其中的元素。 我需要
我试图通过这样做来收集文档中的所有“li”: var first = document.getElementById("scroll-container").getElementsByTagName(
如何在另一个自定义元素的影子 dom 中迭代一个自定义元素的实例? HTMLCollections 似乎没有按预期运行。 (我是一个 jQuerian 和 vanilla js 的新手,所以我确定我在
我是 typescript 的新手,我正在尝试迭代 document.getElementsByClassName() 获取的 HTMLCollection。我的代码是: let tag_list =
我明白HTMLCollection实际上不是一个数组,否则它会被定义为一个数组。我使用一个名为 isArray() 的帮助函数来检测对象是否为数组。我到处都在使用这个小 helper ,在检查 htm
这个问题在这里已经有了答案: Why does jQuery or a DOM method such as getElementById not find the element? (6 个答案)
我正在尝试填充 Element.prototype.children应该返回 HTMLCollection 有一个window.HTMLCollection 不过 var h = new HTMLCo
我有特性搜索功能,可以让我搜索卧室数量、浴室数量、价格(高和低),并且我还试图让它告诉我客户搜索的词是否在记录。比如“河流”之类的。 我一直在使用 .contains() 和 .indexOf(),但
我正在尝试收集此页面上有评级的所有产品的评级:https://www.theluxelens.com/pages/photoshop-overlays 。虽然我可以让下面的代码在我自己的浏览器 (Ch
在 HTML 中我有 9 s 类为“.square”。 我想让它们可点击,我想知道点击了多少次和哪一个并将其存储到数组中。 所以,如果我点击第五个 应点击 5 次[0,0,0,0,0,5,0,0,0
我是一名优秀的程序员,十分优秀!