고향집뚝배기
jquery에서 테이블 짝수, 홀수 번째 TR 배경색 변경하기
여수공동체
jQuery
0
1203
2014.10.22 11:43
jquery에서 테이블(표) 짝수 또는 홀수 번째 TR 배경색 변경하는 방법입니다.
$(document).ready(function(){
$('table tr:odd').css("backgroundColor","#fff"); // odd 홀수
$('table tr:even').css("backgroundColor","#f5f5fc"); // even 짝수
$('table tr:even').css("backgroundColor","#f5f5fc"); // even 짝수
});
또는
$(document).ready(function(){
jQuery('tr').each(function(i) {
this.style.backgroundColor = (i % 2) ? 'red' : 'blue';
});
});
this.style.backgroundColor = (i % 2) ? 'red' : 'blue';
});
});
※ class 사용
$('.tr_bg:odd').css("backgroundColor","#fff");