admin管理员组文章数量:1315365
I'm trying to apply css classes to a calendar after it's been loaded. I've confirmed everything in the javascript and the method in the controller works as it should until I try to get the rows from the tbody. here's the javascript.
jQuery(document).ready(function() {
var calendar = $('#Trafikkalender');
var date = $('#selectedDate').val();
var param = { date: date }
var url = $('#calArrayPostUrl').data('url');
$.post(url, param, function(data) {
var body = calendar.find('tbody');
//var rows = body.getElementsByTagName('tr');
var rows = body.rows;
for (var i = 0; i < rows.length; i++) {
//var cols = rows[i].getElementsByTagName('td');
var cols = rows[i].cells;
for (var j = 0; cols.length; j++) {
var col = cols[j];
col.addClass(data[i][j]);
}
}
});
});
when I hover over rows in the debugger in chrome it dsays that rows is undefined. as you can see I've tried another approach but getElementsByTagName is not a function according to chrome.
I'm trying to apply css classes to a calendar after it's been loaded. I've confirmed everything in the javascript and the method in the controller works as it should until I try to get the rows from the tbody. here's the javascript.
jQuery(document).ready(function() {
var calendar = $('#Trafikkalender');
var date = $('#selectedDate').val();
var param = { date: date }
var url = $('#calArrayPostUrl').data('url');
$.post(url, param, function(data) {
var body = calendar.find('tbody');
//var rows = body.getElementsByTagName('tr');
var rows = body.rows;
for (var i = 0; i < rows.length; i++) {
//var cols = rows[i].getElementsByTagName('td');
var cols = rows[i].cells;
for (var j = 0; cols.length; j++) {
var col = cols[j];
col.addClass(data[i][j]);
}
}
});
});
when I hover over rows in the debugger in chrome it dsays that rows is undefined. as you can see I've tried another approach but getElementsByTagName is not a function according to chrome.
Share Improve this question edited Jul 25, 2016 at 7:56 Álvaro González 147k45 gold badges278 silver badges376 bronze badges asked Jul 25, 2016 at 7:14 user2283330user2283330 4-
Are you aware that jQuery has an
.addClass()
method? – Álvaro González Commented Jul 25, 2016 at 7:22 - look at the bottom of the inner for loop – user2283330 Commented Jul 25, 2016 at 7:23
-
If that function is actually defined (which I don't think so) it cannot be
jQuery.addClass()
becausecol
is not a jQuery object. I posed an answer with the details. – Álvaro González Commented Jul 25, 2016 at 10:37 - ah yes, I have since changed cols. it is now var col = $(cols[j]); – user2283330 Commented Jul 26, 2016 at 8:57
3 Answers
Reset to default 3You can use this
$.post(url, param, function(data) {
var body = calendar.find('tbody');
var rows = body.find('tr');
for (var i = 0; i < rows.length; i++) {
var cols = $(rows[i]).find('td');
for (var j = 0; cols.length; j++) {
var col = cols[j];
col.addClass(data[i][j]);
}
}
}
Hope this helps!!
You can use find()
method passing the tag name:
var rows = body.find('tr');
Same can be used for td
elements too.
body
is a jQuery object, not a DOM element:
var calendar = $('#Trafikkalender');
var body = calendar.find('tbody');
... so it doesn't have a rows
property:
> console.log(body.rows);
undefined
Since you are already using jQuery you can fetch all items at once with a jQuery selector:
body("td")
And if you prefer raw DOM elements you can extract them from the jQuery object:
body[0].rows
本文标签: jqueryHow do I get the rows and columns from a tbody in javascriptStack Overflow
版权声明:本文标题:jquery - How do I get the rows and columns from a tbody in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741975502a2408098.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论