using the DOM I have
document.getElementById('table').rows.length > 4
To find the the tables.
How would this 开发者_StackOverflow中文版be done in jquery while also putting a black border around this table.
$('table').filter(function() {
return $(this).children('tbody').children('tr').length > 4;
}).css('border', '2px solid black');
i.e find all tables, and filter only those which have more than four tr
inside their tbody
.
See http://jsfiddle.net/alnitak/YnVck/
If you don't care about the difference between thead
and tbody
then the simpler:
$('table').filter(function() {
return this.rows.length > 4;
}).css('border', '2px solid black');
from @Felix Kling's comment is simpler.
You could do:
if($('#table tr').length > 4) {
$('#table').css('border', '1px solid black');
}
Note that the selector in the Felix's comment is correct.if
statement depends on your markup. For example, if your tr's are in a tbody
, then it becomes #table tbody tr
.
$('table').each(function() {
if ($(this).children('tr').length > 4) {
$(this).css('border', '5px solid #FFF');
}
}
Might work, untested though.
EDIT: The other answers look much better. They remind me why I like SO (:
精彩评论