我想选择 table
的 td
和 th
。目前我只能选择td
或 th
。我怎样才能缩短它?
$table.find('tr th:nth-child(' + colIndex + ')').css('color', 'red');
$table.find('tr td:nth-child(' + colIndex + ')').css('color', 'red');
最佳答案
如果 TD 包含您不需要钻得太深的数据,那么在 :nth-child
之前使用直接子选择器:
$table.find('tr > :nth-child(' + colIndex + ')').css('color', 'red');
JSFiddle(基于@Rory McCrossan 的): http://jsfiddle.net/TrueBlueAussie/o8wwf1ze/1/
关于javascript - 在 jQuery 中选择多种元素类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29624798/