JQuery hover method and toggle method User Guide, jquerytoggle

Source: Internet
Author: User

JQuery hover method and toggle method User Guide, jquerytoggle

JQuery provides some methods (such as toggle) to combine the effects of the two events, such as mouseover, mouseout, keyup, and keydown.

1. hover Functions

Hover (over, out) is a method that imitates a hover event (move the mouse over an object and remove it from it. This is a custom method that provides a "Keep in it" status for frequently used tasks.
Parameters:
Over (Function): the Function to be triggered when you move the cursor over an element.
Out (Function): the Function to be triggered by removing the mouse from the element.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ("# Panel h5.head"). hover (function (){
$ (This). next (). show (); // triggered when the cursor is suspended
}, Function (){
$ (This). next (). hide (); // triggered when the mouse leaves
})
})
</Script>

2. toggle Function

Toggle (fn, fn) switches the function to be called each time it is clicked. If a matching element is clicked, the specified first function is triggered. When the same element is clicked again, the specified second function is triggered. The subsequent clicks repeatedly call the two functions. You can use unbind ("click") to delete it.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ("# Panel h5.head"). toggle (function (){
$ (This). next (). show (); // triggered when you click it for the first time
}, Function (){
$ (This). next (). hide (); // triggered when the second click is clicked.
})
})
</Script>

The toggle () method switches the visible state of the element.
If the selected elements are visible, these elements are hidden. If the selected elements are hidden, these elements are displayed. The toggle () method switches the visible state of the element.
If the selected elements are visible, these elements are hidden. If the selected elements are hidden, these elements are displayed.

Therefore, the above Code can also be written as follows:

Copy codeThe Code is as follows:
<Script type = "text/javascript">
$ (Function (){
$ ("# Panel h5.head"). toggle (function (){
$ (This). next (). toggle ();
}, Function (){
$ (This). next (). toggle ();
})
})
/* $ (Function (){
$ ("# Panel h5.head"). click (function (){
$ (This). next (). toggle ();
})
})*/
</Script>

You can also add some css styles:

Copy codeThe Code is as follows:
<Style type = "text/css">
. Highlight {background: # FF3300 ;}
</Style>
<Script type = "text/javascript">
$ (Function (){
$ ("# Panel h5.head"). toggle (function () {// used with css styles
$ (This). addClass ("highlight ");
$ (This). next (). show ();
}, Function (){
$ (This). removeClass ("highlight ");
$ (This). next (). hide ();
});
})
</Script>

Do you have a new understanding of common hover events and toggle events in jQuery? I hope this article will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.