简体   繁体   English

单独的组件Angular2中的替代div样式

[英]Alternate div styles within separate components Angular2

I'm trying to apply a striped look to a few divs. 我正在尝试将条纹外观应用于一些div。 My structure is below. 我的结构如下。 Each component is a new instance that is being rendered in an ngFor loop, like this: 每个组件都是一个新实例,正在ngFor循环中呈现,如下所示:

<div *ngFor="let data of dataSet>
  <component1 [data]="data"></component1>
</div>

Rendered HTML 呈现的HTML

<component1>
  <div>
    <div class="style-this-div">
      some content
    </div>
  </div>
</component1>

<component1>
  <div>
    <div class="style-this-div">
      some content
    </div>
  </div>
</component1>

<component1>
  <div>
    <div class="style-this-div">
      some content
    </div>
  </div>
</component1>

Here is what I have tried: 这是我尝试过的:

CSS 的CSS

  div[class^="style-this-div"]:nth-of-type(odd) {background-color: #efeeee;}
  div[class^="style-this-div"]:nth-of-type(even) {background-color: #efeeee;}

  .style-this-div:nth-child(odd) {background-color: #efeeee;}
  .style-this-div:nth-child(even) {background-color: #efeeee;}

When I use the odd selectors, the gray background color is applied to all of them. 当我使用奇数选择器时,灰色背景色将应用于所有这些选择器。 I'm trying to get them to alternate colors. 我正在尝试让它们替代颜色。 What can I do? 我能做什么? Thanks! 谢谢!

You'll need to wrap your content in a div with an ID of component1, component2, etc... and Class of component. 您需要将内容包装在div中,其ID为component1,component2等...以及component的类。 The reason the nth-child isn't working is because it's looking for the odd ".style-this-div" in each component and not in the document.This way we target the first component class of it's kind and style the div inside. nth-child不起作用的原因是因为它在每个组件中而不是在文档中寻找奇数的“ .style-this-div”。这样,我们将目标定位为该类的第一个组件类,并在其中设置div的样式。

If you can't change the structure just add the class of .component to your top level element and the css below should still work. 如果您无法更改结构,只需将.component类添加到顶级元素中,下面的css应该仍然可以使用。

Let me know if you have questions. 如果您有任何问题,请告诉我。

HTML 的HTML

<div id="component1" class="component">
  <div>
    <div class="style-this-div">
      some content
    </div>
  </div>
</div>

<div id="component2" class="component">
  <div>
    <div class="style-this-div">
      some content
    </div>
  </div>
</div>

CSS 的CSS

  .component:nth-child(odd) .style-this-div {background-color: #efeeee;}
  .component:nth-child(even) .style-this-div {background-color: #fff;}

Codepen Here Codepen在这里

Add a class to the component container like i did below. 像我下面一样,将一个类添加到组件容器。 ANd use the "nth-child" odd and even 并使用奇数和偶数的“第n个孩子”

 .component1:nth-child(odd) .style-this-div{ background: red; } .component1:nth-child(even) .style-this-div{ background: yellow; } 
 <component1 class="component1"> <div> <div class="style-this-div"> some content </div> </div> </component1> <component1 class="component1"> <div> <div class="style-this-div"> some content </div> </div> </component1> <component1 class="component1"> <div> <div class="style-this-div"> some content </div> </div> </component1> 

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

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