diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/nodelist/length | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/nodelist/length')
-rw-r--r-- | files/zh-cn/web/api/nodelist/length/index.html | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/nodelist/length/index.html b/files/zh-cn/web/api/nodelist/length/index.html new file mode 100644 index 0000000000..468c9226fb --- /dev/null +++ b/files/zh-cn/web/api/nodelist/length/index.html @@ -0,0 +1,45 @@ +--- +title: NodeList.length +slug: Web/API/NodeList/length +tags: + - 属性 +translation_of: Web/API/NodeList/length +--- +<div>{{APIRef("DOM")}}</div> + +<h2 id="Summary" name="Summary">摘要</h2> + +<p>返回 <a href="/zh-CN/docs/Web/API/NodeList">NodeList</a> <code>集合中</code>子节点数量<code>。</code></p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="brush: js"><em>numItems</em> =<em>nodeList</em>.length +</pre> + +<ul> + <li><code>numItems</code> <code>是一个整数,表示</code> <code>NodeList</code> <code>子节点的数量。</code></li> +</ul> + +<h2 id="Example" name="Example">例子</h2> + +<pre class="brush: js">// 获取文档中的所有 p 标签 +var items = document.getElementsByTagName("p"); + +// 循环 items 然后输出每个 p 标签 html +var gross = ""; +for (var i = 0; i < items.length; i++) { + gross += items[i].innerHTML; +} + +// gross 现在集合了所有 p 标签的 HTML 内容。 +</pre> + +<h2 id="Notes" name="Notes">注意</h2> + +<p>length 不是 <a href="en/DOM/element">元素(Element)</a>的属性,而是 <a href="/zh-CN/docs/Web/API/NodeList">NodeList</a> 的属性。NodeList 是使用 DOM 操作方法返回的对象,比如使用 <a href="en/DOM/document.getElementsByTagName">document.getElementsByTagName</a> 就会返回一个 NodeList 对象。</p> + +<p>length 是在 DOM 操作中非常常见的属性。最常见的是用 length 属性来判断某些节点是否存在,或者如上述一样,用在 for 循环上。</p> + +<h2 id="Specification" name="Specification">规范</h2> + +<p><a class="external" href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-203510337">length</a></p> |