diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/node/isconnected | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/node/isconnected')
-rw-r--r-- | files/zh-cn/web/api/node/isconnected/index.html | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/node/isconnected/index.html b/files/zh-cn/web/api/node/isconnected/index.html new file mode 100644 index 0000000000..f374672b45 --- /dev/null +++ b/files/zh-cn/web/api/node/isconnected/index.html @@ -0,0 +1,93 @@ +--- +title: Node.isConnected +slug: Web/API/Node/isConnected +tags: + - API + - DOM + - Node + - Property + - Reference + - 参考 + - 属性 +translation_of: Web/API/Node/isConnected +--- +<div>{{APIRef("DOM")}}</div> + +<p><strong><code>isConnected</code></strong> 是 {{domxref("Node")}} 的一个只读属性接口。无论节点是否与 DOM 树连接,该属性都会返回一个{{domxref("Boolean", "布尔值")}}。例如: {{domxref("Document")}} 对象与一般 DOM 树连接,{{domxref("ShadowRoot")}} 与 shadow DOM 连接。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>isItConnected</em> = <em>nodeObjectInstance</em>.isConnected</pre> + +<h3 id="返回值">返回值</h3> + +<p>返回 {{domxref("Boolean", "布尔值")}} — 如果该节点与 DOM 树连接则返回 <code>true</code>, 否则返回 <code>false</code>。</p> + +<h2 id="样例">样例</h2> + +<h3 id="标准_DOM_树">标准 DOM 树</h3> + +<pre class="brush: js">let test = document.createElement('p'); +console.log(test.isConnected); // Returns false +document.body.appendChild(test); +console.log(test.isConnected); // Returns true +</pre> + +<h3 id="Shadow_DOM_树">Shadow DOM 树</h3> + +<pre class="brush: js">// 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</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">备注</th> + </tr> + <tr> + <td>{{SpecName('DOM WHATWG','#dom-node-isconnected','isConnected')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + + + +<p>{{Compat("api.Node.isConnected")}}</p> |