值
如果节点与其相关的上下文对象相连,则返回 true,否则返回 false。
示例
标准 DOM
一个标准的 DOM 示例
js
let test = document.createElement("p");
console.log(test.isConnected); // Returns false
document.body.appendChild(test);
console.log(test.isConnected); // Returns true
Shadow DOM
一个 Shadow DOM 示例
js
// Create a shadow root
const shadow = this.attachShadow({ mode: "open" });
// Create some CSS to apply to the shadow DOM
const 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
规范
| 规范 |
|---|
| DOM # ref-for-dom-node-isconnected① |
浏览器兼容性
加载中…