aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/node/isconnected/index.md
blob: 72a68fa202edeac534fc1ddcb1a2cd0425c1758a (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
---
title: Node.isConnected
slug: Web/API/Node/isConnected
tags:
  - プロパティ
  - リファレンス
  - 読み取り専用
browser-compat: api.Node.isConnected
translation_of: Web/API/Node/isConnected
---
{{APIRef("DOM")}}

**`isConnected`** は {{domxref("Node")}} インターフェイスの読み出し専用のプロパティで、ノードがコンテキストオブジェクト、例えば、通常の DOM の場合は {{domxref("Document")}} オブジェクト、シャドウ DOM の場合は {{domxref("ShadowRoot")}} に(直接的または間接的に)接続されているかどうかを示す論理値を返します。

## 値

論理値で、 `true` はこのノードが関連するコンテキストオブジェクトに接続されていることを表し、 `false` は接続していないことを表します。

## 例

### 標準 DOM

標準 DOM の例です。

```js
let test = document.createElement('p');
console.log(test.isConnected); // Returns false
document.body.appendChild(test);
console.log(test.isConnected); // Returns true
```

### シャドウ DOM

シャドウ DOM の例です。

```js
// シャドウルートを生成
const shadow = this.attachShadow({mode: 'open'});

// いくらかの CSS を作成してシャドウ DOM に適用
const 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;
  positions: absolute;
  bottom: 20px;
  left: 10px;
  z-index: 3
}
`;

// 生成された style 要素をシャドウ DOM に取り付ける

shadow.appendChild(style);
console.log(style.isConnected); // Returns true
```

## 仕様書

{{Specifications}}

## ブラウザーの互換性

{{Compat}}

## 関連情報

- [Node.prototype.isConnected polyfill](https://gist.github.com/eligrey/f109a6d0bf4efe3461201c3d7b745e8f)