Jquery-events-off

提供:Dev Guides
移動先:案内検索

jQuery-off(events [、selector] [、handler(eventObject)])メソッド

説明

  • off(events [、selector] [、handler(eventObject)])メソッドは on()*メソッドの反対を行い、バインドされたライブイベントを削除します。

構文

このメソッドを使用するための簡単な構文は次のとおりです-

selector.on( event, selector, handler )

パラメーター

これは、この方法で使用されるすべてのパラメータの説明です-

  • events -スペースで区切られたイベントタイプ。
  • selector -セレクター文字列
  • handler -一致した要素の各セットでイベントにバインドする関数

以下は、このメソッドの使用方法を示す簡単な例です。

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript"
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>

      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {

            function aClick() {
               $("div").show().fadeOut("slow");
            }

            $("#bind").click(function () {
               $("#theone").on("click", aClick).text("Can Click!");
            });

            $("#unbind").click(function () {
               $("#theone").off("click", aClick).text("Does nothing...");
            });

         });
      </script>

      <style>
         button { margin:5px; }
         button#theone { color:red; background:yellow; }
      </style>
   </head>

   <body>

      <button id = "theone">Does nothing...</button>
      <button id = "bind">Bind Click</button>
      <button id = "unbind">Unbind Click</button>

      <div style = "display:none;">Click!</div>
   </body>
</html>

これにより、次の結果が生成されます–