最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery Change Table Cell And Row Background Color On Hover - Stack Overflow

programmeradmin8浏览0评论

Basically when I hover over a row in my table i want the background color of the row to change to say 'black' and the specific cell or td I am hovering over to change to 'red'.

Ie so two events occur when hovering. I know how to do it for one or the other but not both.

Is this possible using jquery?

Thx to everyone for their contribution, I've repped you all.

Basically when I hover over a row in my table i want the background color of the row to change to say 'black' and the specific cell or td I am hovering over to change to 'red'.

Ie so two events occur when hovering. I know how to do it for one or the other but not both.

Is this possible using jquery?

Thx to everyone for their contribution, I've repped you all.

Share Improve this question edited Jul 29, 2013 at 16:49 Nuvolari asked Jul 29, 2013 at 16:04 NuvolariNuvolari 1,1555 gold badges14 silver badges31 bronze badges 2
  • 2 Is there a reason you can't just use css? jsfiddle/4STXa – Jason P Commented Jul 29, 2013 at 16:09
  • If you are eable to color a cell, in the same function, why do not get the parent row and color it too ? Like $(this).addClas("red"); $(this).closest("tr").addClass("black"); – TCHdvlp Commented Jul 29, 2013 at 16:09
Add a ment  | 

7 Answers 7

Reset to default 3

Simple CSS is enough:

tr:hover{
 background-color:red
}

td:hover{
background-color:blue
}

http://jsfiddle/nRuXn/1/

$('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
});

Add some class to that td that you want to be red (lets call that class 'rdClass') and the row 'blkClass':

<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");
    });
});

Add a hover listener to all rows and td's that adds and removes a class, then use CSS to style that class differently for a row and cell.

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;
}

If both the row and cell are in the same container, you could attach a mouseover event to that container and modify the children in the handler.

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

$(function() {
	$(".tablecell").on('mouseover', function(event) {  
		$(".tablerow td").removeClass('hoveColBgColor hoveRowBgColor');

		$(this).parent('tr.tablerow').children('td:gt(0)').addClass('hoveRowBgColor');
		$('.tablerow > td:nth-child('+($(this).index()+1)+')').addClass('hoveRowBgColor');
		
	});
	$(".tablerow").on('mouseout', function(event) {  
		$(".tablerow").children('td:gt(0)').removeClass('hoveRowBgColor');
		$(".tablerow td").removeClass('hoveColBgColor hoveRowBgColor');
	});
});
.hoveRowBgColor{
	background-color:#ccc;
}
.hoveColBgColor{
	background-color:#666;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table id="table1" width="100%" cellspacing="1" cellpadding="0" bordercolor="" border="0">
  <tbody>
    <tr class="tablerow">
      <td class="whiteZone">&nbsp;</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
      <td class="whiteZone">head</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone" style="width:130px;word-wrap: anywhere;">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone" style="width:130px;word-wrap: anywhere;">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone" style="width:130px;word-wrap: anywhere;">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
    <tr class="tablerow">
      <td class="menuZone" style="width:130px;word-wrap: anywhere;">head</td>
      <td class="whiteZone tablecell" id="tablecell_1" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_13" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_4" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_3" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_2" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_12" align="center">test</td>
      <td class="whiteZone tablecell" id="tablecell_18" align="center">test</td>
    </tr>
  </tbody>
</table>

发布评论

评论列表(0)

  1. 暂无评论