Javascript ShadowRoot返回为Null-ShadowRoot显示为open

Javascript ShadowRoot返回为Null-ShadowRoot显示为open,javascript,html,Javascript,Html,我试图在Javascript中选择阴影根,以便为内部元素设置样式。但是当使用elem.shadowRoot时,它返回Null,尽管shadowRoot是下一个元素。下面我粘贴了调试和结果图片 这是我第一次使用shadowRoot,所以我可能会遗漏一些明显的东西 console.log("target"); console.log(target); console.log("target.firstChild"); console.log(target.firstChild); console

我试图在Javascript中选择阴影根,以便为内部元素设置样式。但是当使用elem.shadowRoot时,它返回Null,尽管shadowRoot是下一个元素。下面我粘贴了调试和结果图片

这是我第一次使用shadowRoot,所以我可能会遗漏一些明显的东西

console.log("target");
console.log(target);

console.log("target.firstChild");
console.log(target.firstChild);

console.log("target.shadowroot");
console.log(target.shadowRoot);

console.log("target.firstChild.shadowroot");
console.log(target.firstChild.shadowRoot);

console.log("ha-card");
console.log(target.querySelector("ha-card"));

代码示例的目标是,target.shadowroot而不是shadowroot元素本身


还可以查看使用firstChild方法的文档。这可能是定位不正确。

我很抱歉,最初的答案不正确。我已经编辑过了,如果你也包含html,那就太好了。这样它就可以被正确地观察。