Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between hover () and toggle () in jQuery

Shulou Source: shulou.com Published: 2022-06-01 03:46:10 10月25日 Update

This article mainly introduces what is the difference between hover () and toggle () in jQuery, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

What's the difference between hover () and toggle () in jQuery?

Hover (fn1,fn2): a method that mimics hovering events (moving the mouse over and out of an object). When the mouse moves over a matching element, the first function specified is triggered. When the mouse moves out of this element, the second function specified is triggered.

/ / set class to over when the mouse is over a row of the table, and out when leaving. $("tr") .hover (function () {$(this) .addClass ("over");}, function () {$(this) .addClass ("out");})

Toggle (evenFn,oddFn): toggles the function to be called each time you click. If a matching element is clicked, the first function specified is triggered, and when the same element is clicked again, the second function specified is triggered. Each subsequent click repeats the call to these two functions.

/ / rotate to add and delete a class named selected with each click. $("p") .toggle (function () {$(this) .addClass ("selected");}, function () {$(this) .removeClass ("selected");}) Thank you for reading this article carefully. I hope the article "what's the difference between hover () and toggle () in jQuery" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Functions elements articles mouse objects mobile two events values interests again at the same time methods more friends knowledge editing industry tables information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Docker MySQL Shulou Information