问题描述
提供关于.item()属性的错误答案Web/API/Node/childNodes" rel="nofollow noreferrer">Node.childNodes
对于一个问题,我检查了返回的 childNodes 的
并找到了 __proto__
form
元素的 forEach
方法.
After providing an incorrect answer concerning the .item()
property of Node.childNodes
for a question, I inspected __proto__
of the returned childNodes
of a form
element and found a forEach
method.
Node.childNodes
的 forEach
方法没有记录在 NodeList
, 在 MDN 的 Methods
或 接口节点列表,使用 forEach 方法迭代 NodeList 或链接到该问题的页面;尽管它似乎在 Chromium 50 中可用.
The forEach
method of Node.childNodes
is not documented in the specification of NodeList
, in Methods
at MDN, or Interface NodeList, and does not appear to be mentioned in Iterate a NodeList using forEach method or pages linked to that Question; though it appears available in Chromium 50.
该方法是否仅适用于相对较新版本的 Chrome/Chromium?如果是,是否记录在案?
Is the method available only at relatively recent versions of Chrome / Chromium? If yes, is this documented?
有没有关于Node.childNodes
的forEach()
方法的文档?
Is there any documentation concerning the forEach()
method of Node.childNodes
?
document.querySelector("form").addEventListener("submit", function(e) {
e.preventDefault();
var form = e.target;
form.childNodes.forEach(function(el) {
if (el.tagName === "INPUT" && el.type !== "submit")
snippet.log("name:" + el.name + ", value:" + el.value)
});
});
<form>
<input type="text" name="firstName" value="The first name">
<input type="text" name="lastName" value="The last name">
<input type="email" name="emailAddress" value="email@example.com">
<br>
<input type="submit" value="Submit">
</form>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
推荐答案
DOM4 现在定义了 NodeList 作为可迭代对象:
DOM4 now defines NodeList as an iterable:
iterable<Node>;
根据IDL 草案,这意味着
接口可以声明为可迭代 使用iterable 声明(匹配 Iterable
) 在界面主体中.
iterable<value-type>;
iterable<key-type, value-type>;
实现一个接口的对象被声明为可迭代的支持迭代获取值序列.
Objects implementing an interface that is declared to be iterable support being iterated over to obtain a sequence of values.
注意:在 ECMAScript 语言绑定中,一个接口是iterable 将有entries"、forEach"、keys"、values"和@@iterator 其 属性href="http://heycam.github.io/webidl/#dfn-interface-prototype-object" rel="noreferrer">接口原型对象.
如果给出单个类型参数,则接口具有值迭代器并提供指定类型的值.
If a single type parameter is given, then the interface has a value iterator and provides values of the specified type.
这篇关于Node.childNodes 的 forEach 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!