Use of the toggle () function in jQuery _ jquery

Source: Internet
Author: User
I believe everyone knows about the toggle () function in jQuery very well. There are also many related tutorials on the internet. what I want to share with you today is toggle () in actual projects () for more information, see. Today I met an interesting example to record it.

A level-1 menu contains a level-2 menu, which links page elements through the anchor. When you click an anchor, the page is linked to the corresponding anchor, and the level-2 menu is hidden. click the level-1 menu to continue ..

It has tried a lot of methods, and finally implemented it by reading jquery's toggle function.

// Write $ (". nav "). toggle (function () {$ (". content "). slideToggle () ;}); // you cannot write $ (". nav "). toggle (function () {$ (". content "). slideDown () ;}, function () {$ (". content "). slideUp () ;}); // you can write $ (". nav "). click (function () {$ (". content "). toggle ("slow ");});

Additional instructions for toggle ()

Toggle (fn, fn)

Switch the function to be called each time you click.
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.

Return value
JQuery

Parameters

Fn (Function): the Function to be executed when an odd number of clicks is performed.

Fn (Function): the Function to be executed when an even number of clicks is performed.

Example

Switches a table class.

JQuery code:

$("td").toggle( function () { $(this).addClass("selected"); }, function () { $(this).removeClass("selected"); } );

Note: toggle is only available when you click it. hover is only an event of moving the mouse in and out. it does not matter if you click it. Two can be used together

Related Article

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.