未捕获的TypeError:$(...)。children(...)[0] .css不是函数

时间:2015-08-21 09:12:54

标签: javascript jquery css

我在jQuery选择器中遇到这个错误,当我试图获取我的表时所有的行' for循环中的高度值。

这是我用来遍历表格所有行的选择器:

for (var i = 0; i < myTable.rows.length; i++) {
    var currentRow = $('#table-' + currentGraphOptions.id +' > tbody').children()[i];

    var rowsHeight = currentRow.css('height');
}

标题上的错误是rowsHeight的结果。

这是完整的错误:

Uncaught TypeError: $(...).children(...)[0].css is not a function
at Scope.eval (eval at evaluate (unknown source), <anonymous>:1:65)
at Object.InjectedScript._evaluateOn (<anonymous>:905:55)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:838:34)
at Object.InjectedScript.evaluateOnCallFrame (<anonymous>:964:21)
at Scope.controllers.controller.$scope.saveDashboard (http://localhost:9001/scripts/controllers/DashboardCtrl.js:497:61)
at $parseFunctionCall (http://localhost:9001/bower_components/angular/angular.js:12404:18)
at ngEventDirectives.(anonymous function).compile.element.on.callback (http://localhost:9001/bower_components/angular/angular.js:21566:17)
at Scope.$get.Scope.$eval (http://localhost:9001/bower_components/angular/angular.js:14466:28)
at Scope.$get.Scope.$apply (http://localhost:9001/bower_components/angular/angular.js:14565:23)
at HTMLAnchorElement.<anonymous> (http://localhost:9001/bower_components/angular/angular.js:21571:23)

2 个答案:

答案 0 :(得分:4)

这是因为您正在使用DOM节点而不是jQuery对象。请改用.children().eq(i)

答案 1 :(得分:3)

您从jQuery对象获取DOM元素,而DOM元素没有.css()方法。

您可能需要使用.eq()

for (var i = 0; i < myTable.rows.length; i++) {
    var currentRow = $('#table-' + currentGraphOptions.id +' > tbody').children().eq(i);
    var rowsHeight = currentRow.css('height');
}