开发者

How to change table cell background in Javascript

开发者 https://www.devze.com 2023-01-19 06:57 出处:网络
I have such table in HTML: <table> <tr> <td>Cell 1</td> </tr> 开发者_如何学Go<tr>

I have such table in HTML:

<table>
    <tr>
        <td>Cell 1</td>
    </tr>
 开发者_如何学Go   <tr>
        <td>Cell 2</td>
    </tr>
    <tr>
        <td>Cell 3</td>
    </tr>
</table>

How to change cell's background on mouse move in this cell? If cursor moves away from the cell, background must stay, but if the cursor move to the other cell, it must reset background.


since you need one cell highlighted, if you're including jQuery you could use this code.

(function() {

   var current_cell;

   $('td').bind('mouseenter', function() {
      if (current_cell) {
         current_cell.removeAttr('id');
      };
      current_cell = $(this);
      current_cell.attr('id', 'highlight');
   });
})();

and then just use a bit of css

td#highlight {
  background: ... ;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消