Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a hover state with jQuery?

Tags:

jquery

css

Please, considere this CSS code:

a { color: #ffcc00; } a:hover { color: #ccff00; } 

This HTML code:

<a href="#" id="link">Link</a> <button id="btn">Click here</button> 

And, finally, this JS code:

$("#btn").click(function() {    $("#link").trigger("hover"); }); 

I would like to make my link uses its pseudo-class :hover when the button is clicked. I tried to trigger events like mousemove, mouseenter, hover etc, but anyone works. Notice that I want to force the use of the my CSS pseudo-class :hover specification and not use something like:

$("#link").css("color", "ccff00"); 

Some one know how do I do this? Thank you a lot.

like image 603
MCardinale Avatar asked Feb 18 '10 17:02

MCardinale


People also ask

How do you force a hover state?

Right-click your element. Choose "Force element state" and then the state you want (e.g. :hover) Dance.

How can write hover function in jQuery?

The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element. Syntax: $(selector). hover(Function_in, Function_out);

Is hover deprecated in jQuery?

hover() is deprecated #66.

How do you hover Javascript?

The hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.


1 Answers

You will have to use a class, but don't worry, it's pretty simple. First we'll assign your :hover rules to not only apply to physically-hovered links, but also to links that have the classname hovered.

a:hover, a.hovered { color: #ccff00; } 

Next, when you click #btn, we'll toggle the .hovered class on the #link.

$("#btn").click(function() {    $("#link").toggleClass("hovered"); }); 

If the link has the class already, it will be removed. If it doesn't have the class, it will be added.

like image 173
Sampson Avatar answered Nov 26 '22 09:11

Sampson