开发者

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

开发者 https://www.devze.com 2023-03-14 05:12 出处:网络
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 arou

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 ifstatement 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 (:

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号