本文概述
事件关闭方法从对象中删除回调函数或所有事件。
句法:
object.off(event, callback function, [context])
参数说明
事件:用于绑定对象。
callback:每当触发事件时执行。它引用了代码。
上下文:这是一个可以传递给回调函数的对象。
让我们举个例子。
请参阅以下示例:
<!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 Guys'}, Backbone.Events);
var myFunc = function () {
document.write('Hello Guys');
};
var myFunc1 = function () {
document.write('This is srcmini');
};
//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 display 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文件中,然后在浏览器中打开此文件。
评论前必须登录!
注册