简体   繁体   English

从元素及其子元素中删除所有CSS属性

[英]Remove all CSS properties from an element and its children

The title says it all. 标题说明了一切。 Is it possible to make an element and all its children lose all CSS properties? 是否有可能使一个元素及其所有子元素失去所有CSS属性?

Demo: 演示:

<div class="class-defined-but-should-not-affect-the-div">
<!-- content + children -->
</div>

CSS: CSS:

.class-defined-but-should-not-affect-the-div {
    background-color: green;
}

.class-defined-but-should-not-affect-the-div > .content {
   /* tons of other properties */
}

This example is just to make you understand, the actual situation is different. 这个例子只是为了让您理解,实际情况有所不同。 The element, that I want to STAY AWAY from CSS properties consists of tons of child elements. 我想远离CSS属性的元素由大量子元素组成。 I can't gather all the classes and ids and undo the effects. 我无法收集所有的类和ID并撤消效果。 I need some dynamic solution, probably via JS. 我需要一些动态解决方案,可能是通过JS。

EDIT: Without removing the class and id attributes. 编辑:不删除类和id属性。

Any help would be appreciated. 任何帮助,将不胜感激。

You can use the all property to unset all properties. 您可以使用all属性来unset所有属性。

 body { font-size: small; background-color: #F0F0F0; color:blue; } blockquote { background-color: skyblue; color: red; } blockquote { all: unset; } 
 <blockquote id="quote">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</blockquote> Phasellus eget velit sagittis. 

Snippet from linked source 来自链接源的代码段

You cannot just simply remove CSS properties of an element. 您不能只删除元素的CSS属性。 You can overwrite them or remove its class or id . 您可以覆盖它们或删除其classid

The best approach is to have a toggle class. 最好的方法是有一个切换类。

 setTimeout(()=>{ document.getElementsByClassName('class-defined-but-should-not-affect-the-div')[0].classList.remove('toggle'); }, 2500); 
 .class-defined-but-should-not-affect-the-div { font-size: 20px; color: grey; } .toggle.class-defined-but-should-not-affect-the-div{ background-color: red; padding: 5px; } .toggle.class-defined-but-should-not-affect-the-div * { /* all children */ background-color: blue; padding: 5px; } .toggle.class-defined-but-should-not-affect-the-div p { background-color: yellow; } 
 <div class="toggle class-defined-but-should-not-affect-the-div"> <div> <p>Text</p> </div> </div> 

You can establish all: initial; 您可以建立all: initial; inside parent div for restoring defaul CSS inside it. 在父div内部,用于恢复其中的默认CSS。 Once done that, you can establish new CSS styles on its childrens. 完成后,您可以在其子级上建立新的CSS样式。

Here is a definition of CSS all clause: 这是CSS all子句的定义:

The all property resets all properties, apart from unicode-bidi and direction, to their initial or inherited value (all: initial | inherit | unset;). all属性将除unicode-bidi和direction之外的所有属性重置为其初始值或继承的值(全部:initial | Inherit | unset;)。

  • initial: Changes all the properties applied to the element or the element's parent to their initial value initial:将应用于元素或元素父元素的所有属性更改为其初始值
  • inherit: Changes all the properties applied to the element or the element's parent to their parent value 继承:将应用于元素或元素父级的所有属性更改为其父级值
  • unset: Changes all the properties applied to the element or the element's parent to their parent value if they are inheritable or to their initial value if not 未设置:如果可继承,则将应用于元素或元素父级的所有属性更改为其父级值,否则将其更改为初始值

 body { /* Set CSS for all document */ color: green; font-size: 30px; font-weight: bold; } div { /* Set CSS for all DIVs */ background-color: blue; border: solid 7px black; } .class-defined-but-should-not-affect-the-div { all: initial; /* Set default CSS */ } .class-defined-but-should-not-affect-the-div > .content { /* Set new CSS for DIV childrens with "content" class */ background-color: yellow; color: blue; } 
 <div class="class-defined-but-should-affect-the-div"> I have got CSS styles </div> <div class="class-defined-but-should-not-affect-the-div"> I have not got CSS styles <br/> <label class="content"> I have got new CSS styles</label> </div> 

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

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