下面的例子。我将如何更改 SR 元素中 p 的显示?
#one ?SHADOW-ROOT-QUERY-SELECTOR? p { display: none};
<div>
<p>My Website</p>
<div id="one">
<!--#shadow-root (open)-->
<div id="two">
<div id="three">
<p>Text</p>
</div>
</div>
</div>
</div>
您可以使用子选择器(
#three p
)或直接子选择器(#three > p
)。对于直接子选择器,就像这样:
#three>p {
display: none;
}
<div>
<p>My Website</p>
<div id="one">
<!--#shadow-root (open)-->
<div id="two">
<div id="three">
<p>Text</p>
</div>
</div>
</div>
</div>
<div>
<p>My Website</p>
<div id="one">
<!--#shadow-root (open)-->
<div id="two">
<div id="three">
<p style="display: none;">Text</p>
</div>
</div>
</div>
</div>
或:
<div>
<p>My Website</p>
<div id="one">
<!--#shadow-root (open)-->
<div id="two">
<div id="three">
<p>Text</p>
</div>
</div>
</div>
</div>
<script>
let threeEl = document.getElementById("three");
let threePEl = threeEl.querySelector("p");
threePEl.style.display = "none";
</script>
或者 CascadiaJS 的答案。