我尝试了很多方法,包括这里描述的方法。由于我正在使用外部Web组件库,我无法访问修改这些组件。因此,对我有效的唯一解决方案是使用JS querySelector
,像这样:
document.querySelector("the-element.with-shadow-dom")
.shadowRoot.querySelector(".some-selector").setAttribute("style", "color: black");
这个解决方案并不是最好的,不适用于大型样式,但对于小的增强效果非常有效。
@John,我已经在Chrome 83.0.4103.116上进行了测试(接下来还要在Safari上测试),并且我使用了Ionic(v5)的ion-toast
组件。这里是我使用的(几乎)实际代码:
import { toastController } from '@ionic/core';
let toastOpts = {
message: "Some message goes here.",
cssClass: "toast-with-vertical-buttons",
buttons: [
{
text: "Button 1",
side: 'end'
},
{
text: "Button2",
side: 'end'
},
{
icon: "close",
side: "start"
}
]
}
toastController.create(toastOpts).then(async p => {
let toast = await p.present();
toast.shadowRoot.querySelector('div.toast-button-group-end').setAttribute("style", "flex-direction: column");
});