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

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

问题描述

提供 .item()属性的错误答案的captur / 36105392#36105392 en-US / docs / Web / API / Node / childNodesrel =nofollow noreferrer> Node.childNodes 的一个问题,我检查了 __ proto __ 表单元素返回的 childNodes ,并找到code> 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.

forEach c> Node.childNodes 没有记录在 NodeList 方法在MDN ,或 Interface NodeList ,并不会在 使用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?

有没有关于 forEach()方法的 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 draft ,这意味着


An 界面可以使用$ b声明为 可迭代 $ b 可迭代声明 (与 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语言绑定中,
可迭代的界面将有条目,forEach,keys,values和
@@ iterator 属性在其接口原型对象

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.

如果给出了一个单一的类型参数,那么界面有一个 em> value
iterator
,并提供指定类型的值。

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

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

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