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/css/_colon_defined/index.html | |
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/css/_colon_defined/index.html')
-rw-r--r-- | files/zh-cn/web/css/_colon_defined/index.html | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/files/zh-cn/web/css/_colon_defined/index.html b/files/zh-cn/web/css/_colon_defined/index.html new file mode 100644 index 0000000000..74c0e616c1 --- /dev/null +++ b/files/zh-cn/web/css/_colon_defined/index.html @@ -0,0 +1,113 @@ +--- +title: ':defined' +slug: 'Web/CSS/:defined' +translation_of: 'Web/CSS/:defined' +--- +<div>{{ CSSRef }}</div> + +<p><span class="seoSummary"><strong><code>:defined</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> <a href="/en-US/docs/Web/CSS/Pseudo-classes">伪类</a> 表示任何已定义的元素。这包括任何浏览器内置的标准元素以及已成功定义的自定义元素 (例如通过 {{domxref("CustomElementRegistry.define()")}} 方法)。</span></p> + +<pre class="brush: css no-line-numbers">/* 选择所有已定义的元素 */ +:defined { + font-style: italic; +} + +/* 选择指定自定义元素的任何实例 */ +simple-custom:defined { + display: block; +} +</pre> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">{{csssyntax}}</pre> + +<h2 id="示例">示例</h2> + +<p>下面的片段摘自我们的 <a href="https://github.com/mdn/web-components-examples/tree/master/defined-pseudo-class">定义伪类</a> demo (<a href="https://mdn.github.io/web-components-examples/defined-pseudo-class/">点击查看源码</a>).</p> + +<p>在这个 demo 中我们定义了一个非常简单的自定义元素:</p> + +<pre class="brush: js">customElements.define('simple-custom', + class extends HTMLElement { + constructor() { + super(); + + let divElem = document.createElement('div'); + divElem.textContent = this.getAttribute('text'); + + let shadowRoot = this.attachShadow({mode: 'open'}) + .appendChild(divElem); + } +})</pre> + +<p>然后在文档中插入一个该元素的副本,以及一个标准的 <code><p></code> 标签:</p> + +<pre class="brush: html"><simple-custom text="Custom element example text"></simple-custom> + +<p>Standard paragraph example text</p></pre> + +<p>在 CSS 中我们首先包含以下规则:</p> + +<pre class="brush: css">// 为两个元素设置不同的背景色 +p { + background: yellow; +} + +simple-custom { + background: cyan; +} + +// 将自定义元素和内置元素的字体都设为斜体 +:defined { + font-style: italic; +}</pre> + +<p> </p> + +<p>然后提供以下两个规则来隐藏未定义的自定义元素的所有实例,并显示被定义为块级元素的实例:</p> + +<p> </p> + +<pre class="brush: css">simple-custom:not(:defined) { + display: none; +} + +simple-custom:defined { + display: block; +}</pre> + +<p>这在你有一个复杂的自定义元素需要一段时间才能加载到页面中时非常有用 —— 你可能想要隐藏元素的实例直到定义完成为止,这样你就不会在页面上出现一些难看的元素。</p> + +<h2 id="说明">说明</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{ SpecName('HTML WHATWG', 'semantics-other.html#selector-defined', ':defined') }}</td> + <td>{{ Spec2('HTML WHATWG') }}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("css.selectors.defined")}}</p> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/en-US/docs/Web/Web_Components">Web components</a></li> +</ul> |