【问题标题】:Swap css classes inside every Bootstrap row在每个 Bootstrap 行中交换 css 类
【发布时间】:2017-08-16 21:03:41
【问题描述】:

请找到以下代码,

$scope.datas = [{name:'AB', age:1}, {name:'BC', age:2}, 
{name:'CD', age:3}, {name:'EF', age:4}, {name:'GH', age:5}, 
{name:'IJ', age:6}]

<div ng-repeat="data in datas" class="col-xs-6">
  <span>{{data.name}}</span>
  <span>{{data.age}}</span>
</div>

 .red{
   background-color: red;
  }

.green{
 background-color: green;
}

喜欢

  red    green
  green  red
  red    green

我想使用这些(红色,绿色)类来交换每个引导行。我们怎样才能做到这一点?

【问题讨论】:

  • 您应该为此使用:nth-child,而不是额外的类(以及在此之上具有如此可怕名称的类......)jsfiddle.net/j24yk5L5
  • @CBroe,感谢它在没有 css 类的情况下完美运行,:)

标签: angular-ui-bootstrap mixins bootstrap-4 bootstrap-sass


【解决方案1】:

您应该为此使用:nth-child,而不是额外的课程。

div {
  /* formatting for the “normal” elements goes here (red)
  outline: 1px solid red;
}

div:nth-child(4n+2),  // select every 2nd
div:nth-child(4n+3) { // and 3rd out of each “group” of 4 elements
  /* formatting for the “special” elements, based on their position */
  outline: 1px solid green;
}

演示原理的简单小提琴:https://jsfiddle.net/j24yk5L5/

【讨论】:

  • 实际上我使用的是 bg 颜色而不是轮廓属性,它工作正常。谢谢☺
【解决方案2】:

你可以使用:

<div ng-repeat="data in datas" class="col-xs-6" ng-class="$index % 2 == 0 ? 'even' : 'odd'">
    <span>{{data.name}}</span>
    <span>{{data.age}}</span>
</div>

然后在你的 css 中:

.even span:nth-child(even),
.odd span:nth-child(odd){
    background-color: red;
}
.even span:nth-child(odd),
.odd span:nth-child(even){
    background-color: green;
}

【讨论】:

  • 不行,如果我能用,我上面提到的就买不到了。
  • @Jeyabalan,对不起,误解了问题,已进行编辑
  • 我们可以将css类换成整个div,而不是每个span标签吗?
猜你喜欢
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
  • 2013-09-15
  • 1970-01-01
  • 2015-11-12
相关资源
最近更新 更多