isConnected
是 Node
的一个只读属性接口. 无论节点是否与 DOM 树连接, 该属性都会返回一个Boolean
值. 例如: Document
对象与一般 DOM 树连接, ShadowRoot
与 shadow DOM 连接.
语法
var isItConnected = nodeObjectInstance.isConnected
返回值
返回 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
规范
规范 | 状态 | 备注 |
---|---|---|
DOM isConnected |
Living Standard | 初始定义 |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
特征 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
是否支持 | 54.0 | 53 (53) | ? | 41 | 10.1 |
特征 | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|
是否支持 | 未实现 | 54.0 | 53.0 (53) | ? | 41 | 10.1 | 54.0 |