我的<web-component></webcomponent>是很简单搭建的,<input type="text part="input" />系列直接在#shadow-root.

我可以一口气把它们都做好,没有任何问题:

web-component::part(input) {
  border: 1px solid #000
}

但如果我想要针对特定的输入,这两种方法都不起作用:

web-component::part(input):nth-child(3) {}
web-component::part(input):nth-of-type(3) {}
web-component::part(input).input-3 {} /* with class properly defined in the web component */
web-component::part(input.input-3) {}
web-component::part(input:nth-of-type(3)) {}
...

找不到任何有关此问题的文档.

编辑:我忘了提到,输入不是时隙,它们是动态生成的.

customElements.define("web-component", 
        class extends HTMLElement {
            constructor() {
                super();
                this.attachShadow({mode: "open"});
                this.size = this.getAttribute('size');
                this.template = document.createElement("template");
                this.template.innerHTML = '<style>'
                +':host {white-space: nowrap}'
                +'input {text-align:center;width: 3ch;height: 3ch}'
                +'input:not(:last-child){margin-right:.5ch}'
                +'</style>'
                this.render();
            }

            render() {
            
                this.shadowRoot.appendChild(this.template.content.cloneNode(true));

                for (let i = 0; i < this.size; i++) {
                    const input = document.createElement('input');
                    input.setAttribute('part','input');
                    input.classList.add('input-'+(i+1));
                    input.type = "text";
                    this.shadowRoot.appendChild(input);
                }
            }
        }
    );
web-component::part(input) {
  border: 2px solid orange;
}
web-component::part(input):nth-child(1) {
  background: #ff00ff;
}
web-component::part(input):nth-of-type(2) {
  background: #ff0000;
}
web-component::part(input).input-3 {
  background: #00ff00;
}
web-component::part(input.input-4) {
  background: #00ffff;
}
web-component::part(input:nth-of-type(5)) {
  background: #ff00ff;
}
web-component::part(input:nth-chlid(6)) {
  background: #ff0000;
}
<web-component size="6"></web-component>

推荐答案

这件事不能这样做.来自W3C css-shadow-parts spec:

::Part()伪元素可以在其后面附加pseudo个类,如x-Button::Part(Label):Hover,但永远不会与 struct 伪类或与based on tree information匹配的任何其他伪类匹配,而不是与本地元素信息匹配.

您正在try 的所有 Select 器要么不是伪类,要么不是基于树信息的伪类.

幸运的是,部件名的作用类似于类:多个元素可以具有相同的部件名,单个元素可以具有多个部件名.(也来自同一规格.)

因此,您可以使用多个零件名称,例如input input-1.

customElements.define("web-component", 
        class extends HTMLElement {
            constructor() {
                super();
                this.attachShadow({mode: "open"});
                this.size = this.getAttribute('size');
                this.template = document.createElement("template");
                this.template.innerHTML = '<style>'
                +':host {white-space: nowrap}'
                +'input {text-align:center;width: 3ch;height: 3ch}'
                +'input:not(:last-child){margin-right:.5ch}'
                +'</style>'
                this.render();
            }

            render() {
            
                this.shadowRoot.appendChild(this.template.content.cloneNode(true));

                for (let i = 0; i < this.size; i++) {
                    const input = document.createElement('input');
                    input.setAttribute('part','input input-'+(i+1));
                    input.type = "text";
                    this.shadowRoot.appendChild(input);
                }
            }
        }
    );
web-component::part(input input-1)  {
  background: #ff00ff;
}

web-component::part(input) {
  border: 2px solid orange;
}
<web-component size="6" id="shadow-dom-host"></web-component>

Css相关问答推荐

在Angular mat-drawer组件中防止在mat-drawer-innercontainer中添加滚动条

可折叠/可展开的数据表行:如何使Shadcn数据表中的可折叠内容全宽?

R SHINY:FILL CONTAINER=TRUE时调整DT数据表高度,

我的css是我的react 应用程序没有按预期工作

如何在页面顶部创建固定横幅,并在其下方设置滚动条?

使用 flexbox 使图像与内容高度相同

Img 未拉伸以满足所需的纵横比

SASS 中的计算表达式

为什么左浮动上的清除:左也会影响右浮动,以及如何避免它

如何让我的注册按钮在我的网站上像球一样 skip ?

关键帧不能动画回来

具有自动调整大小的自动填充行的 CSS 网格

为什么无论我做什么都不能改变复选框的 colored颜色 ?

Bootstrap 3 Glyphicons CDN

如何计算所需的色调旋转以生成特定 colored颜色 ?

如何仅在一侧设置 CSS 边框?

:last-child 没有按预期工作?

将 webkit 滚动条样式应用于指定元素

HTML浮动右元素顺序

仅使用 css 的换行符(如
)