发布于 2015-08-14 15:16:40 | 86 次阅读 | 评论: 0 | 来源: 网络整理

这个事件将删除回调函数或一个对象的所有事件。

Syntax


object.off(event, callback function, [context])

参数:

  • event:  绑定的对象。
  • callback: 参考代码。
  • context: 一个可以被传递到一个回调函数的对象。

例子


<!DOCTYPE html>
  <head>
    <title>Event Off Example</title>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
  </head>
  <body>
     <script type="text/javascript">
        //Here creating an object 'myVal' and extending with Backbone.Events method
        var myVal = _.extend({name:'hello'}, Backbone.Events);
        var myFunc = function () {
           document.write('Hello');
        };
        var myFunc1 = function () {
           document.write('Welcome to YiiBai');
        };

        //The on() method will bind the callback function to objects 'myFunc' and 'myFunc1'
        myVal.on('log',myFunc);
        myVal.on('log',myFunc1);
        document.write('Before using off event, values will be: ');

        //trigger() method callbacks for the given event and display the text defined in the 'myFunc' and 'myFunc1' functions
        myVal.trigger('log');

        //The off() method removes the callback for 'myFunc' and logs only text of 'myFunc1'
        myVal.off('log',myFunc);

        document.write("<br>");
        document.write('After using off event, values will be: ');
        myVal.trigger('log');
     </script>
   </body>
</html>

输出

让我们进行以下步骤来看看上面的代码工作:

  • 保存上述代码在文件off.html

  • 在浏览器中打开这个HTML文件。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务