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/element/getelementsbyclassname | |
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/element/getelementsbyclassname')
-rw-r--r-- | files/zh-cn/web/api/element/getelementsbyclassname/index.html | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/getelementsbyclassname/index.html b/files/zh-cn/web/api/element/getelementsbyclassname/index.html new file mode 100644 index 0000000000..e8a2047b45 --- /dev/null +++ b/files/zh-cn/web/api/element/getelementsbyclassname/index.html @@ -0,0 +1,89 @@ +--- +title: Element.getElementsByClassName() +slug: Web/API/Element/getElementsByClassName +translation_of: Web/API/Element/getElementsByClassName +--- +<p>{{APIRef}}</p> +<p><strong><code>Element.getElementsByClassName()</code></strong> 方法返回一个即时更新的(live) {{domxref("HTMLCollection")}},包含了所有拥有指定 class 的子元素。当在 document 对象上调用此方法时,会检索整个文档,包括根元素。</p> +<p>相似地,{{domxref("Document.getElementsByClassName", "getElementsByClassName()")}} 方法会在整个文档上执行;它返回指定拥有指定 class 名称的 document 根节点的后代元素。</p> +<h2 id="Syntax" name="Syntax">语法</h2> +<pre class="syntaxbox"><var>var <em>elements</em></var> = <em>element</em>.getElementsByClassName(<em>names</em>);</pre> +<ul> + <li><em><var>elements</var></em> 是一个即时更新的(live){{ domxref("HTMLCollection") }}。</li> + <li><em><var>names</var></em> 是一个字符串,表示要匹配的类名(class names)列表;类名被空白符分隔。</li> + <li><em>element</em> 是文档中的任一 {{domxref("Element")}}。</li> +</ul> +<h2 id="Examples" name="Examples">例子</h2> +<p>获取所有包含class名称为 test 的元素:</p> +<pre class="brush: js">element.getElementsByClassName('test');</pre> +<p>获取所有包含 <code>red</code> 和 <code>test</code> class名的元素:</p> +<pre class="brush: js">element.getElementsByClassName('red test');</pre> +<p>获取 <code>id</code> 为 <code>main</code> 的元素的所有包含一个 <code>test</code> class名的后代元素:</p> +<pre class="brush: js">document.getElementById('main').getElementsByClassName('test');</pre> +<p>可以在任何 {{domxref("HTMLCollection")}} 上面使用 {{jsxref("Array.prototype")}} 的方法,要把 <code>HTMLCollection</code> 作为该方法的上下文对象(this)。下例,查找类名为 <code>test</code> 的元素中的所有 {{HTMLElement("div")}} 元素:</p> +<pre class="brush: js">var testElements = document.getElementsByClassName('test'); +var testDivs = Array.prototype.filter.call(testElements, function(testElement){ + return testElement.nodeName === 'div'; +});</pre> +<h2 id="Browser_Compatibility" name="Browser_Compatibility">规范</h2> +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('DOM WHATWG', '#dom-element-getelementsbyclassname', 'Element.getElementsByClassName()')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> +<h2 id="浏览器兼容性">浏览器兼容性</h2> +<p>{{ CompatibilityTable() }}</p> +<div id="compat-desktop"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }} [1]</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<div id="compat-mobile"> + <table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatUnknown() }} [1]</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + </tr> + </tbody> + </table> +</div> +<p>[1] Prior to Firefox 19, this method was returning a {{domxref("NodeList")}}; it was then changed to reflects the change in the spec.</p> |