aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/matches
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/element/matches
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/element/matches')
-rw-r--r--files/zh-cn/web/api/element/matches/index.html117
1 files changed, 117 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/matches/index.html b/files/zh-cn/web/api/element/matches/index.html
new file mode 100644
index 0000000000..24d5c682ff
--- /dev/null
+++ b/files/zh-cn/web/api/element/matches/index.html
@@ -0,0 +1,117 @@
+---
+title: Element.matches()
+slug: Web/API/Element/matches
+tags:
+ - Element.matches()
+ - js-30-days
+ - matches()
+translation_of: Web/API/Element/matches
+---
+<p>{{APIRef("DOM")}}</p>
+
+<p>如果元素被指定的选择器字符串选择,<strong><code>Element.matches()</code></strong>  方法返回true; 否则返回false。</p>
+
+<div class="warning">
+<p>有一些浏览器使用前缀, 在非标准名称  matchesSelector () 下实现了这个方法!</p>
+</div>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="eval"><em>let result</em> = <em>element.matches(selectorString);</em>
+</pre>
+
+<ul>
+ <li><code>result</code> 的值为 <code>true</code> 或 <code>false</code>.</li>
+ <li><code>selectorString</code> 是个css选择器字符串.</li>
+</ul>
+
+<h2 id="Example" name="Example">例子</h2>
+
+<pre class="brush: html"><code>&lt;ul id="birds"&gt;
+ &lt;li&gt;Orange-winged parrot&lt;/li&gt;
+ &lt;li class="endangered"&gt;Philippine eagle&lt;/li&gt;
+ &lt;li&gt;Great white pelican&lt;/li&gt;
+&lt;/ul&gt;
+
+&lt;script type="text/javascript"&gt;
+ var birds = document.getElementsByTagName('li');
+
+ for (var i = 0; i &lt; birds.length; i++) {
+ if (birds[i].matches('.endangered')) {
+ console.log('The ' + birds[i].textContent + ' is endangered!');
+ }
+ }
+&lt;/script&gt;</code></pre>
+
+<pre class="brush: js">
+&lt;div id="foo"&gt;This is the element!&lt;/div&gt;
+ &lt;script type="text/javascript"&gt;
+ var el = document.getElementById("foo");
+ if (el.mozMatchesSelector("div")) {
+ alert("Match!");
+ }
+ &lt;/script&gt;
+</pre>
+
+<p>会显示弹出框,因为"div"选择器可以选择到el元素.</p>
+
+<h2 id="异常">异常</h2>
+
+<dl>
+ <dt><code>SYNTAX_ERR</code> {{ gecko_minversion_inline("2.0") }}</dt>
+ <dd>如果给定的css选择器无效. 在 Gecko 2.0之前,该方法会返回<code>false,2.0之后</code>,会直接抛出异常.</dd>
+</dl>
+
+<h2 id="替代方案(Polyfill)" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">替代方案(Polyfill)</h2>
+
+<p>对于不支持 <code style="font-style: normal;">Element.matches()</code> 或<code><span style="font-family: consolas,monaco,andale mono,monospace;">Element.matchesSelector(),但支持</span></code><span style="font-family: consolas,monaco,andale mono,monospace;">document.querySelectorAll()方法的</span><code><span style="font-family: consolas,monaco,andale mono,monospace;">浏览器,存在以下替代方案</span></code></p>
+
+<pre class="brush: js">
+if (!Element.prototype.matches) {
+ Element.prototype.matches =
+ Element.prototype.matchesSelector ||
+ Element.prototype.mozMatchesSelector ||
+ Element.prototype.msMatchesSelector ||
+ Element.prototype.oMatchesSelector ||
+ Element.prototype.webkitMatchesSelector ||
+ function(s) {
+ var matches = (this.document || this.ownerDocument).querySelectorAll(s),
+ i = matches.length;
+ while (--i &gt;= 0 &amp;&amp; matches.item(i) !== this) {}
+ return i &gt; -1;
+ };
+}</pre>
+
+<blockquote>
+<p>关于Polyfill的补充:</p>
+
+<ul>
+ <li><a href="http://en.wikipedia.org/wiki/Polyfill">Polyfill Wikipedia</a></li>
+ <li><a href="http://www.moreonfew.com/what-are-polyfills-in-javascript/">What are Polyfills in Javascript ?</a></li>
+</ul>
+</blockquote>
+
+<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('Selectors API Level 2', '#matches', 'Element.matches')}}</td>
+ <td>{{Spec2('Selectors API Level 2')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性" style="margin-bottom: 20px; line-height: 30px;">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("api.Element.matches")}}</p>