返回值:jQuerytoggle(handler(eventObject), handler(eventObject), [handler(eventObject)])

Bind two or more handlers to the matched elements, to be executed on alternate clicks.

The .toggle() method binds a handler for the click event, so the rules outlined for the triggering of click apply here as well.

For example, consider the HTML:
<div id="target">
  Click here
</div>

Event handlers can then be bound to the <div>:

$('#target').toggle(function() {
  alert('First handler for .toggle() called.');
}, function() {
  alert('Second handler for .toggle() called.');
});

As the element is clicked repeatedly, the messages alternate:

First handler for .toggle() called.
Second handler for .toggle() called.
First handler for .toggle() called.
Second handler for .toggle() called.
First handler for .toggle() called.

If more than two handlers are provided, .toggle() will cycle among all of them. For example, if there are three handlers, then the first handler will be called on the first click, the fourth click, the seventh click, and so on.

Note: jQuery also provides an animation method named .toggle() that toggles the visibility of elements. Whether the animation or the event method is fired depends on the set of arguments passed.

The .toggle() method is provided for convenience. It is relatively straightforward to implement the same behavior by hand, and this can be necessary if the assumptions built into .toggle() prove limiting. For example, .toggle() is not guaranteed to work correctly if applied twice to the same element. Since .toggle() internally uses a click handler to do its work, we must unbind click to remove a behavior attached with .toggle(), so other click handlers can be caught in the crossfire. The implementation also calls .preventDefault() on the event, so links will not be followed and buttons will not be clicked if .toggle() has been called on the element.

示例:

Click to toggle highlight on the list item.

<!DOCTYPE html>
<html>
<head>
<style>
  ul { margin:10px; list-style:inside circle; font-weight:bold; }
  li { cursor:pointer; }
  </style>
<script src="jquery.min.js"></script>
</head>
<body>

<ul>
    <li>Go to the store</li>
    <li>Pick up dinner</li>
    <li>Debug crash</li>

    <li>Take a jog</li>
  </ul>

<script>


    $("li").toggle(
      function () {
        $(this).css({"list-style-type":"disc", "color":"blue"});
      },
      function () {
        $(this).css({"list-style-type":"disc", "color":"red"});
      },
      function () {
        $(this).css({"list-style-type":"", "color":""});
      }
    );



</script>
</body>
</html>
演示:

示例:

To toggle a style on table cells:

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

返回值:jQuerytoggle([duration], [callback])

Display or hide the matched elements.

With no parameters, the .toggle() method simply toggles the visibility of elements:

$('.target').toggle();

The matched elements will be revealed or hidden immediately, with no animation, by changing the CSS display property. If the element is initially displayed, it will be hidden; if hidden, it will be shown. The display property is saved and restored as needed. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

When a duration is provided, .toggle() becomes an animation method. The .toggle() method animates the width, height, and opacity of the matched elements simultaneously. When these properties reach 0 after a hiding animation, the display style property is set to none to ensure that the element no longer affects the layout of the page.

duration 参数可以提供一个毫秒数,代表动画运行的时间,时间越长动画越慢。还可以提供字符串 'fast''slow' ,分别对应了 200600 毫秒。如果没有设置 duration 参数,或者设置成其他无法识别的字符串,就会使用默认值 400 毫秒。

Note: The event handling suite also has a method named .toggle(). Which one is fired depends on the set of arguments passed.

从 jQuery 1.4.3 起,增加了一个可选的参数,用于确定使用的缓冲函数。缓冲函数确定了动画在不同的位置的速度。jQuery默认只提供两个缓冲效果:swinglinear。更多特效需要使用插件。可以访问 jQuery UI 网站 来获得更多信息。

如果提供了回调函数,那么当动画结束时,会调用这个函数。通常用来几个不同的动画依次完成。这个函数不接受任何参数,但是 this 会设成将要执行动画的那个元素。如果对多个元素设置动画,那么要非常注意,回调函数会在每一个动画完成的元素上都执行一次,而不是对这组动画整体才执行一次。

比如下面这个页面:

<div id="clickme">
  Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />

We will cause .toggle() to be called when another element is clicked:

$('#clickme').click(function() {
  $('#book').toggle('slow', function() {
    // 动画完成
  });
});

With the element initially shown, we can hide it slowly with the first click:

A second click will show the element once again:

The second version of the method accepts a Boolean parameter. If this parameter is true, then the matched elements are shown; if false, the elements are hidden. In essence, the statement:

$('#foo').toggle(showOrHide);

is equivalent to:

if (showOrHide) {
  $('#foo').show();
} else {
  $('#foo').hide();
}

示例:

Toggles all paragraphs.

<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
</head>
<body>

<button>Toggle</button>
<p>Hello</p>
<p style="display: none">Good Bye</p>

<script>



$("button").click(function () {
$("p").toggle();
});


</script>
</body>
</html>
演示:

示例:

Animates all paragraphs to be shown if they are hidden and hidden if they are visible, 用时 600 毫秒。

<!DOCTYPE html>
<html>
<head>
<style>
p { background:#dad;
font-weight:bold;
font-size:16px; }
</style>
<script src="jquery.min.js"></script>
</head>
<body>

<button>Toggle 'em</button>

<p>Hiya</p>
<p>Such interesting text, eh?</p>

<script>


$("button").click(function () {
$("p").toggle("slow");
});    


</script>
</body>
</html>
演示:

示例:

Shows all paragraphs, then hides them all, back and forth.

<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
</head>
<body>

<button>Toggle</button>
<p>Hello</p>
<p style="display: none">Good Bye</p>

<script>



var flip = 0;
$("button").click(function () {
$("p").toggle( flip++ % 2 == 0 );
});


</script>
</body>
</html>
演示: