: 나는
fiddle example
에 주석을 추가 한
for (var z = 3; z < rows.length; z++) {
for (var a = 1; a < rows[z].cells.length; a++) {
if (rows[2].cells[a - 1].innerHTML == "S") {
rows[z].cells[a].style.backgroundColor = "#c1c1c1";
}
}
}
이 코드는 많은 세포를 통해 루프를 필요로하지 것처럼 성능에 약간 더 낫다
나는 스타일링을 위해 JavaScript를 사용하는 것을 권장하지 않습니다. 대신, 가능한 한 많은 CSS를 사용하여 성능을 높이고 스크립트 종속성을 낮게 유지하십시오.
테이블 구조가 다음과 같다고 가정합니다. 나는 당신의 스크린 샷에서 다시 최선을 시도 :
<table data-start-day="sun">
<thead>
<tr>
<th>Year</th>
</tr>
<tr>
<th rowspan="2">Month</th>
<th>1</th><!-- fill in --><th>31</th>
</tr>
<tr>
<th>S</th><th>M</th><!-- fill in -->
</tr>
</thead>
<tbody>
<tr>
<td>Employee</td>
<td></td><!-- x days in month -->
</tr>
<tr>
<td>Exceptions</td>
<td></td><!-- x days in month -->
</tr>
</tbody>
</table>
다음으로 우리는 supported in IE 9 and up이다 화합물 선택기의 시리즈를 사용합니다.
:
table[data-start-day=sat] thead tr:last-child th:nth-of-type(7n-13),
table[data-start-day=sat] thead tr:last-child th:nth-of-type(7n-12),
table[data-start-day=sat] tbody tr:nth-of-type(2n) :nth-of-type(7n-12):not(:first-child),
table[data-start-day=sat] tbody tr:nth-of-type(2n) :nth-of-type(7n-11):not(:first-child),
table[data-start-day=fri] thead tr:last-child th:nth-of-type(7n-12),
table[data-start-day=fri] thead tr:last-child th:nth-of-type(7n-11),
table[data-start-day=fri] tbody tr:nth-of-type(2n) :nth-of-type(7n-11):not(:first-child),
table[data-start-day=fri] tbody tr:nth-of-type(2n) :nth-of-type(7n-10):not(:first-child),
table[data-start-day=thu] thead tr:last-child th:nth-of-type(7n-11),
table[data-start-day=thu] thead tr:last-child th:nth-of-type(7n-10),
table[data-start-day=thu] tbody tr:nth-of-type(2n) :nth-of-type(7n-10):not(:first-child),
table[data-start-day=thu] tbody tr:nth-of-type(2n) :nth-of-type(7n-9):not(:first-child),
table[data-start-day=wed] thead tr:last-child th:nth-of-type(7n-10),
table[data-start-day=wed] thead tr:last-child th:nth-of-type(7n-9),
table[data-start-day=wed] tbody tr:nth-of-type(2n) :nth-of-type(7n-9):not(:first-child),
table[data-start-day=wed] tbody tr:nth-of-type(2n) :nth-of-type(7n-8):not(:first-child),
table[data-start-day=tue] thead tr:last-child th:nth-of-type(7n-9),
table[data-start-day=tue] thead tr:last-child th:nth-of-type(7n-8),
table[data-start-day=tue] tbody tr:nth-of-type(2n) :nth-of-type(7n-8):not(:first-child),
table[data-start-day=tue] tbody tr:nth-of-type(2n) :nth-of-type(7n-7):not(:first-child),
table[data-start-day=mon] thead tr:last-child th:nth-of-type(7n-8),
table[data-start-day=mon] thead tr:last-child th:nth-of-type(7n-7),
table[data-start-day=mon] tbody tr:nth-of-type(2n) :nth-of-type(7n-7):not(:first-child),
table[data-start-day=mon] tbody tr:nth-of-type(2n) :nth-of-type(7n-6):not(:first-child),
table[data-start-day=sun] thead tr:last-child th:nth-of-type(7n-7),
table[data-start-day=sun] thead tr:last-child th:nth-of-type(7n-6),
table[data-start-day=sun] tbody tr:nth-of-type(2n) :nth-of-type(7n-6):not(:first-child),
table[data-start-day=sun] tbody tr:nth-of-type(2n) :nth-of-type(7n-5):not(:first-child){
background:#CCC;
}
결과는 원하는 출력과 일치 : 주 전원을 참고 우리는 토/일 열에게 그들이 일정 자체에 가을에 상관없이 타겟팅 할 수있는 :nth-of-type
의 사용을 통해
바이올린 : jQuery로 http://jsfiddle.net/80fajvd6/4/
테이블을 만들 위치에 코드를 추가하십시오. 생성시 클래스를 지정하는 것이 모든 요소를 반복하고 테이블 채우기 후에 수행하는 것보다 간단 할 수 있습니다. – Zaphod
테이블의 전체 렌더링 된 HTML을 포함 할 수 있습니까? 하루 헤더가 시작되기 전에 두 행이있는 것처럼 보입니다. – Pete
예, 첫 번째 행은 Year (2014)이고 두 번째 행은 Month (12 월 rowspan)입니다. 2), 세 번째 행에는 날짜가 포함되어 있으며 (항상 1부터 시작), 네 번째는 날짜 헤더입니다. – Laureant