简体   繁体   English

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

[英]how to hide an inside element in js dom

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

this will show the text "123",My purpose is once I hide a element,all children will hide too,but why not? 这将显示文本“ 123”。我的目的是一旦我隐藏了一个元素,所有的孩子也都会隐藏,但是为什么不呢? In javascript,I can get the children of "p" tag and hide each one.it's too troublesome. 在javascript中,我可以获取“ p”标记的子代并隐藏每个子代。这太麻烦了。 I want a simple way,like "display:none;withChildren:all" once and for all. 我想要一种简单的方法,例如"display:none;withChildren:all" Is there any way? 有什么办法吗?

The <pre> and <p> tag are both HTML block tags, which are not allowed to be nested inside of each other. <pre><p>标记都是HTML块标记,不允许彼此嵌套。 Browser doesn't understand and render them separately, you can check by open developer tool to check the HTML code. 浏览器无法单独理解和呈现它们,您可以通过打开开发人员工具来检查HTML代码。

Instead, you should try using <span> or any inline-block tag. 相反,您应该尝试使用<span>或任何内联块标记。

You could use .children . 您可以使用.children For example. 例如。

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

You could also use CSS selectors. 您也可以使用CSS选择器。 Like so. 像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM