aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/node/isconnected/index.html
blob: a655d7fb31b8efca31a990b27f75fbf509c757e9 (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
---
title: Node.isConnected
slug: Web/API/Node/isConnected
tags:
  - API
  - DOM
  - Node
  - Property
  - Reference
  - 参考
  - 属性
translation_of: Web/API/Node/isConnected
---
<div>{{APIRef("DOM")}}</div>

<p><strong><code>isConnected</code></strong> 是 {{domxref("Node")}} 的一个只读属性接口。无论节点是否与 DOM 树连接,该属性都会返回一个{{domxref("Boolean", "布尔值")}}。例如: {{domxref("Document")}} 对象与一般 DOM 树连接,{{domxref("ShadowRoot")}} 与 shadow DOM 连接。</p>

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

<pre class="syntaxbox">var <em>isItConnected</em> = <em>nodeObjectInstance</em>.isConnected</pre>

<h3 id="返回值">返回值</h3>

<p>返回 {{domxref("Boolean", "布尔值")}} — 如果该节点与 DOM 树连接则返回 <code>true</code>, 否则返回 <code>false</code></p>

<h2 id="样例">样例</h2>

<h3 id="标准_DOM_树">标准 DOM 树</h3>

<pre class="brush: js">let test = document.createElement('p');
console.log(test.isConnected); // Returns false
document.body.appendChild(test);
console.log(test.isConnected); // Returns true
</pre>

<h3 id="Shadow_DOM_树">Shadow DOM 树</h3>

<pre class="brush: js">// Create a shadow root
var shadow = this.attachShadow({mode: 'open'});

// Create some CSS to apply to the shadow dom
var style = document.createElement('style');
console.log(style.isConnected); // returns false

style.textContent = '.wrapper {' +
                       'position: relative;' +
                    '}' +

                     '.info {' +
                        'font-size: 0.8rem;' +
                        'width: 200px;' +
                        'display: inline-block;' +
                        'border: 1px solid black;' +
                        'padding: 10px;' +
                        'background: white;' +
                        'border-radius: 10px;' +
                        'opacity: 0;' +
                        'transition: 0.6s all;' +
                        'position: absolute;' +
                        'bottom: 20px;' +
                        'left: 10px;' +
                        'z-index: 3;' +
                      '}' +

// Attach the created style element to the shadow dom

shadow.appendChild(style);
console.log(style.isConnected); // Returns true</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM WHATWG','#dom-node-isconnected','isConnected')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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

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