最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: $(...).children(...)[0].css is not a function - Stack Overflow

programmeradmin1浏览0评论

I am getting this error in my jQuery selector when I am trying to get my table's all rows' height value in a for loop.

This is the selector I use to traverse all rows of the table:

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

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

The error on the title is as a result of rowsHeight.

This is full error:

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_ponents/angular/angular.js:12404:18)
at ngEventDirectives.(anonymous function)pile.element.on.callback (http://localhost:9001/bower_ponents/angular/angular.js:21566:17)
at Scope.$get.Scope.$eval (http://localhost:9001/bower_ponents/angular/angular.js:14466:28)
at Scope.$get.Scope.$apply (http://localhost:9001/bower_ponents/angular/angular.js:14565:23)
at HTMLAnchorElement.<anonymous> (http://localhost:9001/bower_ponents/angular/angular.js:21571:23)

I am getting this error in my jQuery selector when I am trying to get my table's all rows' height value in a for loop.

This is the selector I use to traverse all rows of the table:

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

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

The error on the title is as a result of rowsHeight.

This is full error:

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_ponents/angular/angular.js:12404:18)
at ngEventDirectives.(anonymous function).pile.element.on.callback (http://localhost:9001/bower_ponents/angular/angular.js:21566:17)
at Scope.$get.Scope.$eval (http://localhost:9001/bower_ponents/angular/angular.js:14466:28)
at Scope.$get.Scope.$apply (http://localhost:9001/bower_ponents/angular/angular.js:14565:23)
at HTMLAnchorElement.<anonymous> (http://localhost:9001/bower_ponents/angular/angular.js:21571:23)
Share asked Aug 21, 2015 at 9:12 brainmassagebrainmassage 1,2627 gold badges23 silver badges46 bronze badges 2
  • Try wrapping currentRow in $: var rowsHeight = $(currentRow).css('height'); – Jakub Bilko Commented Aug 21, 2015 at 9:14
  • When accessing element from the array jQuery uses to store then you take the pure DOM element so you lose all jQuery methods attach to a jQuery insteance object. – GillesC Commented Aug 21, 2015 at 9:16
Add a ment  | 

2 Answers 2

Reset to default 4

It's because you are working with DOM node instead of jQuery object. Use .children().eq(i) instead.

You are getting the DOM element from your jQuery object and DOM elements don't have a .css() method.

You may mean to use .eq():

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

评论列表(0)

  1. 暂无评论