简体   繁体   中英

put border around table with 4 or more rows (tr) using jquery

using the DOM I have

document.getElementById('table').rows.length > 4

To find the the tables.

How would this 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');

ie 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 if statement depends on your markup. For example, if your tr's are in a tbody , then it becomes #table tbody tr . Felix's comment is correct.

$('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 (:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM