aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/document/queryselector
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-tw/web/api/document/queryselector')
-rw-r--r--files/zh-tw/web/api/document/queryselector/index.html102
1 files changed, 102 insertions, 0 deletions
diff --git a/files/zh-tw/web/api/document/queryselector/index.html b/files/zh-tw/web/api/document/queryselector/index.html
new file mode 100644
index 0000000000..a79edb1c1f
--- /dev/null
+++ b/files/zh-tw/web/api/document/queryselector/index.html
@@ -0,0 +1,102 @@
+---
+title: document.querySelector
+slug: Web/API/Document/querySelector
+tags:
+ - DOM
+ - Fixit
+ - Gecko DOM Reference
+ - Gecko DOM 參考
+ - Selectors
+ - 選擇器
+translation_of: Web/API/Document/querySelector
+---
+<div>
+ {{ApiRef()}}</div>
+<h2 id="Summary" name="Summary">摘要</h2>
+<p>回傳 document 第一個符合特定選擇器群組的元素(採用深度優先,前序追蹤 document 節點)。</p>
+<h2 id="Syntax" name="Syntax">語法</h2>
+<pre><em>element</em> = document.querySelector(<em>selectors</em>);</pre>
+<p>其中</p>
+<ul>
+ <li><code>element</code> 是<a href="/en-US/docs/DOM/element" title="en-US/docs/DOM/element">元素</a>物件。</li>
+ <li><code>selectors</code> 是以逗號分隔,包含一個或多個 CSS 選擇器的字串。</li>
+</ul>
+<h2 id="Example" name="Example">範例</h2>
+<p>這個範例會回傳 document 選到的第一個 "<code>myclass</code>" class:</p>
+<pre class="brush: js">var el = document.querySelector(".myclass");
+</pre>
+<h2 id="Notes" name="Notes">注意事項</h2>
+<p>若找不到相應元素就會回傳 <code>null</code>,否則回傳第一個符合的元素。</p>
+<p>若選擇器符合某 ID,且該 ID 在 document 中誤用數次,就會回傳第一個符合的元素。</p>
+<p>當特定選擇器群組無效,會擲回 <code>SYNTAX_ERR</code> 例外狀況。</p>
+<p><code>querySelector()</code> 是由 Selectors API 引入的選擇器。</p>
+<p>傳入 <code>querySelector</code> 的字串參數必須遵循 CSS 語法。若要選取未遵循 CSS 語法的 ID 或選擇器(例如不當使用冒號或空格),必須強制加上兩個反斜線來跳脫錯誤的字元:</p>
+<pre class="brush: html">&lt;div id="foo\bar"&gt;&lt;/div&gt;
+&lt;div id="foo:bar"&gt;&lt;/div&gt;
+
+&lt;script&gt;
+document.querySelector('#foo\bar') // 甚麼都沒選到
+document.querySelector('#foo\\\\bar') // 選到第一個 div
+document.querySelector('#foo:bar') // 甚麼都沒選到
+document.querySelector('#foo\\:bar') // 選到第二個 div
+&lt;/script&gt;
+</pre>
+<h2 id="Browser_Compatibility" name="Browser_Compatibility">瀏覽器相容性</h2>
+<p>{{CompatibilityTable()}}</p>
+<div id="compat-desktop">
+ <table class="compat-table">
+ <tbody>
+ <tr>
+ <th>功能特色</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>基本支援</td>
+ <td>1</td>
+ <td>3.5 (1.9.1)<br>
+ {{bug(416317)}}</td>
+ <td>8</td>
+ <td>10</td>
+ <td>3.2 (525.3)<br>
+ {{Webkitbug("16587")}}</td>
+ </tr>
+ </tbody>
+ </table>
+</div>
+<div id="compat-mobile">
+ <table class="compat-table">
+ <tbody>
+ <tr>
+ <th>功能特色</th>
+ <th>Android</th>
+ <th>Firefox 行動版 (Gecko)</th>
+ <th>IE 行動版</th>
+ <th>Opera 行動版</th>
+ <th>Safari 行動版</th>
+ </tr>
+ <tr>
+ <td>基本支援</td>
+ <td>2.1</td>
+ <td>有</td>
+ <td>9</td>
+ <td>10.0</td>
+ <td>3.2</td>
+ </tr>
+ </tbody>
+ </table>
+</div>
+<h2 id="Specification" name="Specification">規格文件</h2>
+<ul>
+ <li><a class="external" href="http://www.w3.org/TR/selectors-api/" title="http://www.w3.org/TR/selectors-api/">Selectors API</a></li>
+</ul>
+<h2 id="See_also" name="See_also">詳見</h2>
+<ul>
+ <li>{{domxref("document.querySelectorAll()")}}</li>
+ <li>{{domxref("element.querySelector()")}}</li>
+ <li>{{domxref("element.querySelectorAll()")}}</li>
+ <li><a href="/en-US/docs/Code_snippets/QuerySelector" title="en-US/docs/Code snippets/QuerySelector">querySelector 代碼片段</a></li>
+</ul>