19 Feb jQuery hover() method
Definition
Use the jQuery hover() method, to hover over a matched element and call two functions, one for mouseenter and another for mouseleave.
Syntax
The following is the syntax,
1 2 3 |
$(selector).hover(enterFunction,leaveFunction) |
The following are the parameters,
- enterFunction: Function to run when the mouseenter event occurs. This is a required parameter.
- leaveFunction: Function to run when the mouseleave event occurs. This is an optional parameter.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $("h2").hover(function(){ $(this).css("color", "blue"); }); }); </script> </head> <body> <h2>Hover over me to change my color.</h2> <p>Understanding the usage of hover() method in JavaScript.</p> </body> </html> |
The following is the output,
Hover over the heading to change its color. The color will change to blue,
No Comments