【发布时间】:2022-01-24 17:16:54
【问题描述】:
在我的 HTML 中,我有:
<link rel="stylesheet" href="css/styles.css">
<script src="js/components/custom.js"></script>
...
<custom></custom>
在custom.js,假设我已经定义:
class Custom extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({mode: 'open'});
shadow.innerHTML = `
<style>
@import url('/css/styles.css');
</style>
...
`
}
}
customElements.define('custom', Custom)
在styles.css 中,当我定义样式时,它们起作用。但我不能做类似的事情
custom img {
}
以custom 内的img 为目标。我必须使用 div.custom-contents 之类的包装器来定位其中的元素。
这是一个已知的限制,可能是 @imports 这样的自定义元素,还是我做错了什么?
【问题讨论】:
标签: html css web-component custom-element css-import