using jquery to select table columns greater than n?
Is there a way in jquery to select all td columns greater than say 4? I have a table with 16 columns and I initially only want the first 4 shown.
I know I can use:
$('tr td:nth-child(5)').hide();
to hide them one at a time of put a class on all of them but it would be nice if there wa开发者_StackOverflows a simpler jquery way to do it.
$('tr').each(
function(){
$(this).find('td:gt(3)').hide();
});
Demo at: http://jsfiddle.net/davidThomas/7SDpr/.
You can use the :gt()
selector to do this:
$('tr td:gt(4)').hide();
Documentation
EDIT:
As Patrick pointed out, this won't work for every row. You'll need to use .each to apply this for each row.
$('tr').each(
function() {
$(this, ).find('td:gt(4)').hide();
});
You can do it in an .each()
using .slice()
like this:
Example: http://jsfiddle.net/kEByC/
$('tr').each(function() {
$(this).children().slice(3).hide();
});
or like this using .nextAll()
:
Example: http://jsfiddle.net/kEByC/1
$('tr > td:nth-child(3)').nextAll().hide();
Try this:
$('tr > td:nth-child(n+3)').hide();
精彩评论