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

问题描述

提供的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 没有记录在,,或,并不会在 或链接到该问题的页面;虽然它可能在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 protected]">
  <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现在定义作为一个迭代:

DOM4 now defines NodeList as an iterable:

iterable<Node>;

根据,这意味着

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.

如果给出了一个单一的类型参数,那么界面有一个 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的每个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 05:05