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/document/getelementsbyclassname/index.html | |
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/document/getelementsbyclassname/index.html')
-rw-r--r-- | files/zh-cn/web/api/document/getelementsbyclassname/index.html | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/document/getelementsbyclassname/index.html b/files/zh-cn/web/api/document/getelementsbyclassname/index.html new file mode 100644 index 0000000000..ea090774ad --- /dev/null +++ b/files/zh-cn/web/api/document/getelementsbyclassname/index.html @@ -0,0 +1,129 @@ +--- +title: Document.getElementsByClassName() +slug: Web/API/Document/getElementsByClassName +tags: + - API + - DOM + - HTML5 +translation_of: Web/API/Document/getElementsByClassName +--- +<p id="Summary">{{APIRef("DOM")}}</p> + +<p>返回一个包含了所有指定类名的子元素的类数组对象。当在document对象上调用时,会搜索整个DOM文档,包含根节点。你也可以在任意元素上调用{{domxref("Element.getElementsByClassName", "getElementsByClassName()")}} 方法,它将返回的是以当前元素为根节点,所有指定类名的子元素。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><var>var elements</var> = document.getElementsByClassName(<em>names</em>); // or: +<var>var elements</var> = rootElement.getElementsByClassName(<em>names</em>);</pre> + +<ul> + <li><var>elements</var> 是一个实时{{ domxref("HTMLCollection", "集合") }},包含了找到的所有元素。</li> + <li><var>names</var> 是一个字符串,表示要匹配的类名列表;类名通过空格分隔</li> + <li>getElementsByClassName 可以在任何元素上调用,不仅仅是 document。 调用这个方法的元素将作为本次查找的根元素.</li> +</ul> + +<h2 id="Examples" name="Examples">示例</h2> + +<p>获取所有 class 为 'test' 的元素:</p> + +<pre class="brush: js">document.getElementsByClassName('test');</pre> + +<p>获取所有 class 同时包括 'red' 和 'test' 的元素.</p> + +<pre class="brush: js">document.getElementsByClassName('red test');</pre> + +<p>在id 为'main'的元素的子节点中,获取所有class为'test'的元素</p> + +<pre class="brush: js">document.getElementById('main').getElementsByClassName('test');</pre> + +<p>我们还可以对任意的 {{ domxref("HTMLCollection") }} 使用 Array.prototype 的方法,调用时传递 <var>HTMLCollection</var> 作为方法的参数。这里我们将查找到所有class为 'test' 的 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> + +<h3 id="获取第一个类名为_test_的元素">获取第一个类名为 test 的元素</h3> + +<p>这是 <code>getElementsByClassName()</code> 的通常用法:</p> + +<pre class="brush: html"><html> +<body> + + <div id="parent-id"> + <p>hello world 1</p> + <p class="test">hello world 2</p> + <p>hello world 3</p> + <p>hello world 4</p> + </div> + + <script> + var parentDOM = document.getElementById("parent-id"); + + var test = parentDOM.getElementsByClassName("test"); // 匹配类名的元素集合,不是元素本身 + console.log(test); //HTMLCollection[1] + + var testTarget = parentDOM.getElementsByClassName("test")[0]; // 我们想要取到的第一个元素 + console.log(testTarget); //<p class="test">hello world 2</p> + </script> +</body> +</html></pre> + +<h2 id="多个_Class_示例">多个 Class 示例</h2> + +<p><code>document.getElementsByClassName</code> 的工作方式与 <code>document.querySelector</code> 和 <code>document.querySelectorAll</code> 很相似。 只有所有 className 都匹配的元素会被选择。</p> + +<h3 id="HTML">HTML</h3> + +<pre class="brush: html"><code><span class="orange fruit">Orange Fruit</span> +<span class="orange juice">Orange Juice</span> +<span class="apple juice">Apple Juice</span> +<span class="foo bar">Something Random</span> +<textarea id="resultArea" style="width:100%;height:7em"></textarea></code></pre> + +<h3 id="JavaScript">JavaScript</h3> + +<pre class="brush: js"><code>// getElementsByClassName selects partial matches +var allOrangeJuiceByClass = document.getElementsByClassName('orange juice'); +var result = "document.getElementsByClassName('orange juice')"; +for (var i=0, len=allOrangeJuiceByClass.length|0; i<len; i=i+1|0) { + result += "\n " + allOrangeJuiceByClass[i].textContent; +} + + +// querySelector only selects full complete matches +var allOrangeJuiceQuery = document.querySelectorAll('.orange.juice'); +result += "\n\ndocument.querySelectorAll('.orange.juice')"; +for (var i=0, len=allOrangeJuiceQuery.length|0; i<len; i=i+1|0) { + result += "\n " + allOrangeJuiceQuery[i].textContent; +} + +document.getElementById("resultArea").value = result;</code></pre> + +<h3 id="结果">结果</h3> + +<p>{{EmbedLiveSample('Multiple_Classes_Example')}}</p> + +<h2 id="Specification" name="Specification">规范</h2> + +<table> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('DOM WHATWG', '#dom-document-getelementsbyclassname', 'document.getElementsByClassName')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.Document.getElementsByClassName")}}</p> |