javascript - 以 Angular 使用 id 访问第 n 个 child

标签 javascript jquery css angularjs google-code-prettify

我正在尝试使用代码美化为特定行着色。我正在从后端获取行号方面的数据,并想突出显示它们。 如果我在我的 css 文件中写这个:

#file2 li:nth-child(n+1):nth-child(-n+11) {
        background: #18b478;
    }

它工作正常。但是,我不知道如何使用 angular 来实现这个,我想根据我得到的行号动态地对 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>

希望这对您有所帮助!

关于javascript - 以 Angular 使用 id 访问第 n 个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47616740/

相关文章:

javascript - Jquery keyup事件没有触发

html - 为什么 css 背景大小会旋转图像?

jquery - 使用 jquery 更改工具提示 div 的 css

c# - 用于刷新页面和/或 telerik 网格的 JavaScript 代码

javascript - jQuery Dom 数字

javascript - JS 中获取 HiddenField 的值并传入 var

jquery - 如何使用 bootstrap 中的 selectpicker 插件在 select 上设置选定值

php - Jquery:选中或取消选中复选框时无法显示加载相应页面

javascript - React Native Scroll View 不滚动也不跳到底部

javascript - 将 HTML 表单输入存储在 JS 对象中