From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/node/isconnected/index.html | 93 +++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 files/zh-cn/web/api/node/isconnected/index.html (limited to 'files/zh-cn/web/api/node/isconnected') diff --git a/files/zh-cn/web/api/node/isconnected/index.html b/files/zh-cn/web/api/node/isconnected/index.html new file mode 100644 index 0000000000..f374672b45 --- /dev/null +++ b/files/zh-cn/web/api/node/isconnected/index.html @@ -0,0 +1,93 @@ +--- +title: Node.isConnected +slug: Web/API/Node/isConnected +tags: + - API + - DOM + - Node + - Property + - Reference + - 参考 + - 属性 +translation_of: Web/API/Node/isConnected +--- +
{{APIRef("DOM")}}
+ +

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

+ +

语法

+ +
var isItConnected = nodeObjectInstance.isConnected
+ +

返回值

+ +

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

+ +

样例

+ +

标准 DOM 树

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

Shadow DOM 树

+ +
// 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
+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('DOM WHATWG','#dom-node-isconnected','isConnected')}}{{Spec2('DOM WHATWG')}}Initial definition.
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ + + +

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

-- cgit v1.2.3-54-g00ecf