1

I have a dropdown menu and I'am using mouseenter() function. If mouse enters to my selector's area background-color returning to yellow. But ıf I leave from my selector's area I want to set default colour of my selector's area without using mouseleave() function.

How can I fix it?

$(document).ready(function(){
    $("#l_ev_men").mouseenter(function(){
        $(this).css("background-color","yellow");
        $(this).css("color","black");
    });

    $("#l_ev_men").mouseleave(function(){ // ı dont want to use this function
    });
});
6
  • 2
    Why don't you want to use mouseleave? Commented Jul 11, 2013 at 15:39
  • You can use .hover() instead of mouse enter/leave - api.jquery.com/hover Commented Jul 11, 2013 at 15:39
  • 3
    Why aren’t you using CSS? Commented Jul 11, 2013 at 15:39
  • because ı have many selectors which they are different type, some of them ul, some of them li, div etc.. Commented Jul 11, 2013 at 15:39
  • 1
    You could use the CSS class #l_ev_men:hover, but browser support varies. What's the reason to use mouseleave? Commented Jul 11, 2013 at 15:39

2 Answers 2

5

I would recomend adding and removing CSS classes.

#l_ev_men:hover, #l_ev_men.hover {
     color: black;
     background-color: yellow;    
}

Not all browsers have support for the :hover pseudo-selector, you can use addClass() and removeClass() , this will match the #l_ev_men.hover selector on the CSS.

$("#l_ev_men").hover(
    function in(){ 
         $(this).addClass("hover"); 
    }, 
    function out(){ 
         $(this).removeClass("hover");  
    }
);
Sign up to request clarification or add additional context in comments.

Comments

3

It's usually better to change the class using toggleClass(). Even better, you can do something this simple without jquery just using the :hover CSS psuedo class.

.hoverable:hover {
    background-color:yellow;
    color:black;
}

then:

<li class="hoverable <otherstuff>">...</li>...

or

<td class="hoverable <otherstuff>">...</td>...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.