Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - leaving the hover state

Tags:

jquery

I have 2 div tag. The first contains a table. The second contains some text. When the page is loaded both div have no background-color. When there is an hover on a td of the first div, the second div background-color is set to yellow (see code below). My problem is that when I leave the table with the mouse, the background of the second div stays yellow. I would like the color to be remove on leaving the hover state. Hope someone can help. Thank you in advance for your replies. Cheers. Marc.

My HTML :

​<div id="div-one">
    <table>
        <tr><td>1</td><td>2</td></tr>
        <tr><td>3</td><td>4</td></tr>
        <tr><td>5</td><td>6</td></tr>
        <tr><td>7</td><td>8</td></tr>                   
    </table>
</div>

​<div id="div-two">
    some text
</div>

My JS:

$(document).on("hover", "#div-one td", function() { 

    $("#div-two").css("background-color":"yellow");
});​
like image 861
Marc Avatar asked Feb 28 '12 11:02

Marc


People also ask

Is hover deprecated in jQuery?

Is hover deprecated in jQuery? Deprecated in jQuery 1.8, removed in 1.9: The name “hover” used as a shorthand for the string “mouseenter mouseleave” .

What's the name of the jQuery event triggered on a slider when a user moves the cursor?

The mouseover event is sent to an element when the mouse pointer enters the element. Any HTML element can receive this event.

What is the jQuery equivalent of Onmouseover?

jQuery mouseover() Method The mouseover() method triggers the mouseover event, or attaches a function to run when a mouseover event occurs. Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element.


1 Answers

Use mouseenter and mouseleave events separately - which is actually a "hover" event:

$(document).on({
    mouseenter: function() {
        $("#div-two").css("background-color", "yellow");
    },
    mouseleave: function() {
        $("#div-two").css("background-color", "");
    }
}, "#div-one td");​

DEMO

like image 50
Didier Ghys Avatar answered Oct 06 '22 04:10

Didier Ghys