简体   繁体   English

如何避免CSS对动态添加的类的影响

[英]How to avoid CSS impact to dynamically added classes

What actually happens : I have a div in a file there is HTML code, which added Dynamically from BackEnd, CSS Classes(which included from Dynamic code) uses CSS from my style.css 实际发生的情况 :我在文件中有一个div ,其中包含HTML代码,该代码是从后端动态添加的,CSS类(包含在动态代码中)使用了我的style.css CSS

What I need : My own CSS should not impact on dynamically added code. 我需要的是 :我自己的CSS不应影响动态添加的代码。

Similar Solution : iframe is a solution but I want to do same using div tag. 相似的解决方案iframe是一种解决方案,但我想使用div标签进行相同的操作。

在此处输入图片说明

我认为这是:

<div class="row" style="all:initial"> do some thing</div>

Better you can write you css with parent-child combination to avoid such issues as like below snippet. 更好的是,您可以将css与父子组合一起编写,以避免出现以下代码段之类的问题。 Here newly added <p> tag has same class .content but parent has different class name as .dynamic from .static . 在这里,新添加的<p>标记具有相同的类.content但parent具有与.static不同的类名称( .dynamic

 jQuery(document).ready(function() { jQuery('.dynamic').html(jQuery(".static").html()); }); 
 .static .content { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="static"> <p class="content">Some content goes here</p> </div> <div class="dynamic"> </div> 

Three options from my perspective: 我认为三个选择:

1. Use unique classnames 1.使用唯一的类名

2. Unique ID 2.唯一ID

If only classes are added, give the parent div an ID and overwrite the css by using the ID. 如果仅添加类,请给父div一个ID,然后使用该ID覆盖CSS。

<div id="uniqueStyle" class="dynamicDiv">
    <div class="row clearfix"></div>
    <div class="row clearfix"></div>
    <div class="row clearfix"></div>
    <div class="row clearfix"></div>
</div>

#uniqueStyle{
 background: red; //overwrite
}

#uniqueStyle .row{
background: blue; //overwrite
}

3. Using :not 3.使用:not

Give the div an unique ID and use the :not selector to exclude it from styling. 为div赋予唯一的ID,然后使用:not选择器将其从样式中排除。

<div id="noStyling" class="dynamicDiv">
   <div class="row clearfix"></div>
   <div class="row clearfix"></div>
   <div class="row clearfix"></div>
   <div class="row clearfix"></div>
</div>

:not(#noStyling){
   background: blue; //set styling of all elements except #noStyling
}

More info about :not selector: https://www.w3schools.com/cssref/sel_not.asp 有关:not选择器的更多信息: https : //www.w3schools.com/cssref/sel_not.asp

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

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