如何在 js 中隐藏内部元素 dom

how to hide an inside element in js dom

<p style="display:none">ppp<pre>123</pre></p>

这会显示文本“123”,我的目的是一旦我隐藏了一个元素,所有children也会隐藏,但为什么不呢? 在javascript中,我可以获取"p"标签的children,隐藏每个one.it太麻烦了。 我想要一个简单的方法,比如 "display:none;withChildren:all" 一劳永逸。 有什么办法吗?

您可以使用 .children。例如。

$("p").children().hide(); // hide all children of `p`
$("p").hide(); // hide `p`

您也可以使用 CSS 选择器。像这样。

$("p *").hide(); // hide all children of `p`
$("p").hide(); // hide `p`

<pre><p>标签都是HTML块标签,不允许相互嵌套。浏览器不理解并单独呈现它们,您可以通过打开开发者工具检查HTML代码。

相反,您应该尝试使用 <span> 或任何行内块标记。