【发布时间】:2020-09-05 11:26:23
【问题描述】:
如果有办法实现以下目标,我很感兴趣:
::slotted(input[type="checkbox"]:disabled) ~ ::slotted(label) {
cursor: not-allowed;
}
通过在某些示例上对其进行测试,它不起作用。
Specification 没有说明这是否应该可行。 MDN 也没有涵盖这种情况。
我不想在shadow-dom 中包含input 和label,因为我不想处理和/或复制这些元素的本机行为。
附:我知道我可以使用 javascript 来做到这一点(例如,通过将类添加到开槽 label),但我正在寻找一个简单的 css 解决方案。
完整示例:
<script>
customElements.define('my-element', class extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<style>
::slotted(input:disabled) ~ ::slotted(label) {
color: red;
}
::slotted(input:disabled) + ::slotted(label) {
color: red;
}
</style>
<slot name="inputel"></slot>
<slot name="inputlabel"></slot>`;
}
});
</script>
<my-element>
<input disabled id="input1" type="text" slot="inputel"/>
<label for="input1" slot="inputlabel">label</label>
</my-element>
【问题讨论】:
-
你能添加完整的 HTML,最好是一个工作的 SO sn-p。
input和label在哪里? -
@Danny'365CSI'Engelman 添加。
-
@lamplightdev 不是真的,但它肯定有一些有趣的见解,谢谢你的链接!
标签: css web-component shadow-dom custom-element