aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/element/matches/index.html
blob: 24d5c682ff789554f211eb50bd199d7f7e5ae9b1 (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
---
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>