我們的志工尚未將本文翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!
您也可以閱讀本文的 English (US) 版本。

The isConnected read-only property of the Node interface returns a boolean indicating whether the Node is connected (directly or indirectly) to the context object, for example the Document object in the case of the normal DOM, or the ShadowRoot in the case of a shadow DOM.

Syntax

var isItConnected = nodeObjectInstance.isConnected

Return value

A Boolean that is true if the node is connected to its relevant context object, and false if not.

Examples

A standard DOM example:

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

A shadow DOM example:

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

 

Specifications

Specification Status Comment
DOM
The definition of 'isConnected' in that specification.
Living Standard Initial definition.

Browser Compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support51 ?53 ?3810.1
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support5151 ?453810.16.0

文件標籤與貢獻者

此頁面的貢獻者: jpmedley, fscholz, chrisdavidmills
最近更新: jpmedley,