Node.childNodes 的 forEach 方法? [英] forEach method of Node.childNodes?

查看:18
本文介绍了Node.childNodes 的 forEach 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供关于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.childNodesforEach 方法没有记录在 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.childNodesforEach()方法的文档?

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) 在界面主体中.

An interface can be declared to be iterable by using an iterable declaration (matching Iterable) in the body of the interface.

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">接口原型对象.

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.

如果给出单个类型参数,则接口具有值迭代器并提供指定类型的值.

If a single type parameter is given, then the interface has a value iterator and provides values of the specified type.

这篇关于Node.childNodes 的 forEach 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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