숨겨진 열이 많은 HTML 테이블이 있습니다. 열은 3 개의 집합으로 생각할 수 있습니다. 처음 두 개는 숨겨지고 세 번째는 볼 수 있습니다. 3 번째에 마우스를 올리면 3 개 세트의 처음 2 개도 볼 수있게됩니다. <th>
마우스 오버가 발생하는 열에서 상위 2 개를 선택하는 방법을 알아 내려고합니다. 그래서 jQuery를 사용하여 html을 변경하여 colspan='3'
둘 다 에 추가 <th>
하여 "세트"의 3 개 열 모두에 걸쳐 있습니다.
예 : 첫 번째 노란색 열의 일부 위에 마우스를 놓으면 해당 열 이전에 숨겨 졌던 2 개의 열이 이제 표시됩니다. "1000017"및 "Potato Chips"는 열 범위를 3으로 변경하여 열을 다시 올바르게 덮을 수 있도록 선택해야합니다. 내 현재 코드가 제대로 작동하지 않고 대신 전체 "항목 이름"행을 선택합니다.
내 질문은 : <th>
전체 행 대신 적절한 것을 선택하도록 this.siblings jQuery 행을 어떻게 수정할 수 있습니까 ?
아래 스 니펫을 참조하십시오.
$('.yellowbr').hover(function() {
var sel = $(this).index();
$('tr').each(function(){
$('td',this).slice(sel-3,sel-1).toggleClass('hidetd');
$(this).siblings().first().toggleClass('light');
})
})
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
.yellowbr {
background-color: yellow;
}
.hidetd {
display:none;
}
.light {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Item #</th>
<th>100017</th>
<th>100018</th>
<th>100019</th>
<th>100020</th>
</tr>
<tr>
<th>Component</th>
<th><u>Potato chips</u>
</th>
<th><u>butterfingers</u>
</th>
<th><u>Flat Pretzels</u>
</th>
<th><u>Milk Chocolate</u>
</th>
</tr>
<tr>
<th>2016-01-03</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-04</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-05</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-06</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-07</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
</table>
다음은 숨겨진 열이없고 모든 제목이 올바른 열을 포함하는 경우의 모든 모습입니다.
.yellowbr {
background-color: yellow;
}
table,th, td {
border: 1px solid black;
border-collapse: collapse;
}
<table>
<tr>
<th>Item #</th>
<th colspan="3">100017</th>
<th colspan="3">100018</th>
<th colspan="3">100019</th>
<th colspan="3">100020</th>
</tr>
<tr>
<th>Component</th>
<th colspan="3"><u>Potato chips</u></th>
<th colspan="3"><u>butterfingers</u></th>
<th colspan="3"><u>Flat Pretzels</u></th>
<th colspan="3"><u>Milk Chocolate</u></th>
</tr>
<tr>
<th>2016-01-03</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-04</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-05</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-06</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-07</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
</table>
이것이 당신이 찾고있는 것입니까?
$('.yellowbr').hover(function() {
var sel = $(this).index();
$('tr').each(function() {
$('th', this).slice(sel / 3, sel / 3 + 1).attr('colspan', 3);
$('th', this).filter(function(i) {
return i !== sel / 3;
}).attr('colspan', 1);
var cellsToHightlight = $('td', this).slice(sel - 3, sel - 1);
for (var i = 0; i < cellsToHightlight.length; i += 1) {
var cell = $(cellsToHightlight[i]);
if (cell.hasClass('hidetd')) {
cell.removeClass('hidetd');
}
}
$('td', this).filter(function(i, el) {
return (i < sel - 3 || i > sel - 1) && (i + 1) % 3 !== 0;
}).addClass('hidetd');
$(this).siblings().first().toggleClass('light');
});
});
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
.yellowbr {
background-color: yellow;
}
.hidetd {
display: none;
}
.light {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Item #</th>
<th>100017</th>
<th>100018</th>
<th>100019</th>
<th>100020</th>
</tr>
<tr>
<th>Component</th>
<th><u>Potato chips</u>
</th>
<th><u>butterfingers</u>
</th>
<th><u>Flat Pretzels</u>
</th>
<th><u>Milk Chocolate</u>
</th>
</tr>
<tr>
<th>2016-01-03</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-04</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-05</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-06</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
<tr>
<th>2016-01-07</th>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
<td class='hidetd'>0</td>
<td class='hidetd'>0</td>
<td class='yellowbr'>0</td>
</tr>
</table>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다