aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/node/contains/index.html
blob: 917c54d3fa5fd5208de27b2a47ed4b264b6f01c9 (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
---
title: Node.contains
slug: Web/API/Node/contains
translation_of: Web/API/Node/contains
---
<div>{{ApiRef}}</div>

<h2 id="Summary" name="Summary">概述</h2>

<p><strong>Node.contains()</strong>返回的是一个布尔值,来表示传入的节点是否为该节点的后代节点。</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox">node.contains( otherNode )
</pre>

<ul>
 <li><code>node</code> 是否包含otherNode节点.</li>
 <li><code>otherNode</code> 是否是node的后代节点.</li>
</ul>

<p>如果 <code>otherNode</code><code>node 的后代节点或是</code> <code>node</code> 节点本身.则返回<code>true</code> , 否则返回 <code>false</code>.</p>

<h2 id="Example" name="Example">例子</h2>

<p>下面的函数用来检查一个元素是否是body元素的后代元素且非body元素本身.</p>

<pre class="brush:js">function isInPage(node) {
  return (node === document.body) ? false : document.body.contains(node);
}</pre>

<ul>
 <li><code>node</code> 是我们想要检查的元素节点.</li>
</ul>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat("api.Node.contains")}}

<h2 id="Specification" name="Specification">规范</h2>

<ul>
 <li><a class="external" href="http://www.w3.org/TR/domcore/#dom-node-contains" title="http://www.w3.org/TR/domcore/#dom-node-contains">DOM Level 4: contains</a></li>
</ul>

<h2 id="See_also" name="See_also">相关链接</h2>

<ul>
 <li>{{domxref("Node.hasChildNodes")}}</li>
</ul>