aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/document/getelementsbyclassname/index.html
blob: b66647c82d80f44c6708ae14b55b93ad629730fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
---
title: Document.getElementsByClassName()
slug: Web/API/Document/getElementsByClassName
tags:
  - 待翻譯
translation_of: Web/API/Document/getElementsByClassName
---
<p id="Summary">{{APIRef("DOM")}}</p>

<p>針對所有給定的 class 子元素,回傳類似陣列的物件。當呼叫 document 物件時,它會搜尋整個文件,包括根節點在內。你也可以在所有元素呼叫 {{domxref("Element.getElementsByClassName", "getElementsByClassName()")}},那它就只會回傳含有給定 class 的特定根元素的後代元素。</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> 為符合 class 名稱的 {{ domxref("HTMLCollection") }}</li>
 <li><var>names</var> 為符合 class 名稱的字串;class 名稱可以用空白分隔。</li>
 <li>getElementsByClassName 可以被任何不只在 document 的元素呼叫。呼叫這個方法的元素將會成為搜尋 class 的根元素。</li>
</ul>

<h2 id="Examples" name="Examples">範例</h2>

<p>取得所有 class 為 “test” 的元素:</p>

<pre class="brush: js">document.getElementsByClassName('test');</pre>

<p>取得所有 class 為 “test” 和 “red” 的元素:</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") }} 為 <var>this </var>來使用 <code>Array.prototype</code> 的方法。下面的例子將會找到所有 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>

<h2 id="取得_class_是_test_的元素">取得 class 是 test 的元素</h2>

<p>這是最常用的操作方法:</p>

<pre>&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id="parent-id"&gt;
        &lt;p&gt;hello word1&lt;/p&gt;
        &lt;p class="test"&gt;hello word2&lt;/p&gt;
        &lt;p&gt;hello word3&lt;/p&gt;
        &lt;p&gt;hello word4&lt;/p&gt;
    &lt;/div&gt;
    &lt;script&gt;
        var parentDOM = document.getElementById("parent-id");

        var test=parentDOM.getElementsByClassName("test");//test is not target element
        console.log(test);//HTMLCollection[1]

        var testTarget=parentDOM.getElementsByClassName("test")[0];//year , this element is target
        console.log(testTarget);//&lt;p class="test"&gt;hello word2&lt;/p&gt;
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>

<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>3.0</td>
   <td>9.0</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() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
   <td>{{ CompatUnknown() }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Specification" name="Specification">規範</h2>

<ul>
 <li><a href="https://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-getelementsbyclassname" title="https://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-getelementsbyclassname"><span class="external">W3C: getElementsByClassName</span></a></li>
</ul>