Home >Web Front-end >JS Tutorial >Detailed explanation of jQuery's very practical event toggle() method
Switch different background colors:
$("p").toggle( function(){ $("body").css("background-color","green");}, function(){ $("body").css("background-color","red");}, function(){ $("body").css("background-color","yellow");} );
Try it yourself
toggle() method is used for binding Two or more event handlers functions to respond to the click event of the selected element in turn.
This method can also be used to switch the hide() and show() methods of the selected element. Bind two or more functions to the Toggle eventWhen the specified element is clicked, switch between two or more functions in turn. If more than two functions are specified, the toggle() method will toggle all functions. For example, if there are three functions, the first click will call the first function, the second click will call the second function, and the third click will call the third function. The fourth click calls the first function again, and so on. Syntax$(selector).toggle(function1(),function2(),functionN(),...)
Description | |
---|---|
function1() | Required. Specifies a function to run when the element is clicked every even number of times.|
function2() | Required. Specifies a function to be run every odd number of times the element is clicked.|
functionN(),... | Optional. Specify other functions that need to be switched.
$(selector).toggle(speed,callback)
Description | |
---|---|
Optional. Specifies the speed of hide/show effects. The default is "0". Possible values:
|
|
Optional. Function executed when the toggle() method completes. |
To learn more about callback, please visit our Callback function tutorial. |
Syntax
$(selector).toggle(switch)
Description | |
---|---|
Required. A Boolean value that specifies whether toggle() should only show or hide all selected elements. |
|
The above is the detailed content of Detailed explanation of jQuery's very practical event toggle() method. For more information, please follow other related articles on the PHP Chinese website!