javascript - 如何使用 jquery 在 Angular 中选择具有 ng-class attr 的元素

标签 javascript jquery angularjs

全部:

假设我有一个类似的模板:

<input ng-repeat="d in data track by $index" ng-class="parent{{$index}}" />

它呈现为

<input ng-repeat="d in data track by $index" ng-class="parent0" class="ng-pristine ng-untouched ng-valid ng-scope">

但是当我使用 $(".parent0") 时,没有选择任何内容。

所以我想知道如何选择 ng-class 指定的元素?

谢谢

最佳答案

表达式应返回一个字符串:

ng-class="'parent{{$index}}'"

使用第一点:

The directive operates in three different ways, depending on which of three types the expression evaluates to:

  1. If the expression evaluates to a string, the string should be one or more space-delimited class names.

  2. If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name.

  3. If the expression evaluates to an array, each element of the array should either be a string as in type 1 or an object as in type 2. This means that you can mix strings and objects together in an array to give you more control over what CSS classes appear. See the code below for an example of this.

Docs

Plunk

关于javascript - 如何使用 jquery 在 Angular 中选择具有 ng-class attr 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34935046/

相关文章:

javascript - 我正在学习 D3.js,但我没有得到与教程相同的控制台输出

c# - 通过 jQuery .append() 添加 ASP.Net 元素

javascript - 列表上的自动换行

javascript - VueJS 独立组件事件

javascript - 将 CSRFToken 添加到 Ajax 请求

javascript - 如何将 HTML block 设置为按钮?

JavaScript 外部 HTML 编码 URL 字符串

javascript - ngOptions 默认选择所有选项并在选择时消失选项

带有数据方法的 HTML 链接不起作用

javascript - jQuery .load() 函数只改变一次内容