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
若不。
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;
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
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. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
节点
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument