以角度访问使用ID的第n个孩子
问题描述:
我想使用代码美化为特定的行着色。我从行号数据中获取数据,并希望突出显示它们。 如果我写这在我的css文件:以角度访问使用ID的第n个孩子
#file2 li:nth-child(n+1):nth-child(-n+11) {
background: #18b478;
}
它的正常工作。但是,我无法弄清楚如何使用角度来实现这一点,我想根据我得到的行号动态地对CSS进行更改。这是我曾尝试:
$("#file2 li:nth-child(n+1):nth-child(-n+11)").css('background', "#18b478");
这是我的html代码:
<div class="container" style="margin-bottom: 50px">
<div class="col-sm-6">
<pre>
<code class="prettyprint linenums" id="file1">***some code***</code>
</pre>
</div>
答
想象一下,你也行的颜色在数组中。 (用行数的长度)并且你在另一个数组中有你的行。
$scope.lines = linesarray
$scope.colors = colorsarray
代码
<pre>
<code ng-repeat="line in $scope.lines">
Line content: {{ line }}
Line number is: {{ $index }}
Associated color is: {{ colors[$index] }}
</code>
//make use of the $index that returns current index of the ng-repeat loop
</pre>
希望这有助于!
使用angular通常意味着不要使用jQuery – charlietfl
希望li:nth-child(n + 1)选择每个li。我对这个选择器有点困惑! –