jQuery toggle()Methods

jQuery 事件方法jQuery Event Methods

Example

Toggle the color when clicking a <p> element:

$("p").toggle( function(){$("p").css({"color":"red"});}, function(){$("p").css({"color":"blue"});}, function(){$("p").css({"color":"green"}); });

Try it Yourself »

Definition and Usage

In jQuery version 1.8, the toggle() methodwas deprecated, and was removed in version 1.9.

The toggle() method adds two or more functions to toggle between in response to the click event of the selected element.

The first specified function is called when the element is clicked, the second when it is clicked again, and so on.

Note:There is also ajQuery Effectsmethod named toggle(). Which method is called depends on the parameters.


Syntax

$(selector).toggle(function)

Parameters Description
function Required. The function to run whenever the selected element is clicked.


实例

More Examples

Toggle between multiple functions
How to toggle between multiple functions.


jQuery 事件方法jQuery Event Methods

Other Extensions