当前位置: 首页 > 文档资料 > jQuery 入门教程 >

off(events [, selector ] [, handler(eventObject) ] )

优质
小牛编辑
113浏览
2023-12-01

描述 (Description)

off( events [, selector ] [, handler(eventObject) ] )方法与on()方法相反,它删除绑定的实时事件。

语法 (Syntax)

以下是使用此方法的简单语法 -

<i>selector</i>.on( event, selector, handler ) 

参数 (Parameters)

以下是此方法使用的所有参数的说明 -

  • events - 以空格分隔的事件类型。

  • selector - 选择器字符串

  • handler - 绑定到每个匹配元素集上的事件的函数

例子 (Example)

以下是一个简单的示例,简单地显示了此方法的用法。

<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>