isConnected 只读特性在 节点 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.

句法

var isItConnected = nodeObjectInstance.isConnected
					

返回值

A 布尔 也就是 true if the node is connected to its relevant context object, and false 若不。

范例

Standard DOM

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
					

Shadow DOM

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;
  positions: 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
					

Polyfill

Node.isConnected can be polyfilled with the following code for IE10 and EdgeHTML:

/*
 * Node.isConnected polyfill for IE and EdgeHTML
 * 2020-02-04
 *
 * By Eli Grey, https://eligrey.com
 * Public domain.
 * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
 */
if (!('isConnected' in Node.prototype)) {
  Object.defineProperty(Node.prototype, 'isConnected', {
    get() {
      return (
        !this.ownerDocument ||
        !(
          this.ownerDocument.compareDocumentPosition(this) &
          this.DOCUMENT_POSITION_DISCONNECTED
        )
      );
    },
  });
}
					

规范

规范 状态 注释
DOM
The definition of 'isConnected' in that specification.
实时标准 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
isConnected Chrome 51 Edge 79 Firefox 53 IE 不支持 No Opera 38 Safari 10.1 WebView Android 51 Chrome Android 51 Firefox Android 45 Opera Android 41 Safari iOS 10.3 Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

元数据

  • 最后修改: