2013-07-29 2 views
2

기본적으로 테이블의 행 위로 마우스를 가져 가면 행의 배경색이 '검은 색'으로 변경되고 특정 셀 또는 TD가 변경되도록 변경합니다. '붉은 색'.JQuery 테이블 셀과 행 배경색 변경 (호버시)

이렇게하면 호버링 할 때 두 가지 이벤트가 발생합니다. 나는 둘 중 하나를 위해 그것을하는 방법을 압니다.

jquery를 사용하면 가능합니까?

Thx는 모든 분들께 기부를 전하고 있습니다.

+2

그냥 CSS를 사용할 수없는 이유가 있나요? http://jsfiddle.net/4STXa/ –

+0

같은 기능으로 셀을 색칠 할 수 있다면 부모 행을 가져 와서 색을 지정하지 않는 이유는 무엇입니까? '$ (this) .addClas ("red")처럼; $ (this) .closest ("tr"). addClass ("black");' – TCHdvlp

답변

3

간단한 CSS는 충분하다 :

tr:hover{ 
background-color:red 
} 

td:hover{ 
background-color:blue 
} 

http://jsfiddle.net/nRuXn/1/

+0

Thx yes는 이전 CSS를 잊어 버렸습니다. 그것은 트릭을했다. – Nuvolari

+0

내 자신의 경험이 거의 없으므로'hover','mouseover-mouseout','mouseenter-mouseleave'에 대해 이야기 할 때마다 CSS 호버 만 생각하기 시작했습니다. – rps

1

행과 셀이 모두 동일한 컨테이너에있는 경우 해당 컨테이너에 mouseover 이벤트를 첨부하고 핸들러에서 자식을 수정할 수 있습니다.

<table> 
<tr class='rdClass'> 
<td> 
     1 
</td> 
<td class='blkClass'> 
     2 
</td> 
<td> 
     3 
</td> 
</tr> 
</table> 

$(document).ready(function() 
{ 
    $(".rdClass").mouseover(function() 
    { 
     $(this).css("background-color", "red"); 
    }); 

    $(".blkClass").mouseover(function() 
    { 
     $(this).css("background-color", "black"); 
    }); 
}); 
2

당신이 빨간색으로 원하는 TD 몇 가지 클래스를 추가하고 행 'blkClass'(클래스 'rdClass'를 호출 할 수 있습니다) 클래스를 제거한 다음 CSS를 사용하여 행과 셀에 대해 클래스의 스타일을 다르게 지정하십시오.

Working Demo

jQuery를

$('tr, td').hover(function() { 
    $(this).addClass('highlight'); 
}, function() { 
    $(this).removeClass('highlight'); 
}); 

CSS

tr.highlight { 
    background-color: red; 
} 

td.highlight { 
    background-color: black; 
} 
2

가 추가 모든 행과 TD의에 호버 리스너를 추가

1
$("td").hover(function(){ 
    $(this).css("background-color", "red"); 
    $(this).parrent('tr').css("background-color", "black"); 
}); 
3
$('td').hover(function() { 
    $(this).parent().children().css("background-color", "black"); 
    $(this).css("background-color", "red") 
}); 

$('tr').mouseleave(function() { 
    $(this).children('td').css("background-color", "white");// or whatever 
});