值
如果節點與其相關的上下文物件相連,則返回 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① |
瀏覽器相容性
載入中…