jQueryのtoggle()メソッド

❮jQueryエフェクトメソッド

すべての<p>要素の非表示と表示を切り替えます。

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

定義と使用法

トグル()メソッドは、選択した要素のhide()show()を切り替えます。

このメソッドは、選択された要素の可視性をチェックします。show()は、要素が非表示の場合に実行されます。要素が表示されている場合はhide()が実行されます-これにより、トグル効果が作成されます。

:非表示の要素はまったく表示されません(ページのレイアウトには影響しなくなりました)。

ヒント:このメソッドは、カスタム関数を切り替えるためにも使用できます。


構文

$(selector).toggle(speed,easing,callback)

Parameter Description
speed Optional. Specifies the speed of the hide/show effect

Possible values:

  • milliseconds
  • "slow"
  • "fast"
easing Optional. Specifies the speed of the element in different points of the animation. Default value is "swing"

Possible values:

  • "swing" - moves slower at the beginning/end, but faster in the middle
  • "linear" - moves in a constant speed
Tip: More easing functions are available in external plugins
callback Optional. A function to be executed after the toggle() method is completed

To learn more about callback, visit our jQuery Callback chapter


自分で試してみてください-例


使用速度パラメーターを使用して、非表示/表示効果の速度を指定する方法。


の使用非表示/表示効果を切り替えるときにコールバックパラメーターを使用する方法。


❮jQueryエフェクトメソッド