开发者

Figuring out how to use hover function in jquery to highlight one cell only

开发者 https://www.devze.com 2022-12-19 05:24 出处:网络
I have this table. I want to change the color of a cell, from black to red, when i move over that cell, I\'m using the .hover method, and i\'m having trouble figuring out how to make work accordingly

I have this table. I want to change the color of a cell, from black to red, when i move over that cell, I'm using the .hover method, and i'm having trouble figuring out how to make work accordingly to what i need.

<html>
    <head>
    <script type="text/javascript" src="jquery-1.4.js"></script>
    <script>
    $board = $('#board')
    $(this).hover(function() {
        $('td').css('border-color', 'red');
开发者_如何学编程    },
    function() {
        $('td').css('border-color', 'black')
    })
    </script>
    <style type="text/css">
    td {
        border-style:solid;
        border-color:black;
    }
    </style>        
    </head>
    <body>
    <table id="board">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <input type="button" value="Shuffle" onclick="change()"/>
    </body>
</html>


you could try something like:

<script type="text/javascript">

$(document).ready(function(){

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

</script>

remember the type="text/javascript" on the script tag

when the page loads the $(document).ready function will be called, then it will set up the hover events.

when the mouse hovers over a td inside #board the first function will be called on this, the td you're hovering over. when the mouse moves out it will call the second function putting the border back to black.


Try this

$board.find('td').hover(function(){
    $(this).css('border-color', 'red');
},
function(){
    $(this).css('border-color', 'black')
})
0

精彩评论

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