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