87,964
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
/*********************
* 预先定义三个函数
*********************/
function func1(msg){
alert(msg);
}
function func2(msg){
alert(msg);
}
function func3(msg){
alert(msg);
}
$(function(){
//这一步顺利执行,依次弹出了相关数据
$(window).on("resize",function(){
func1("1");
func2("2");
func3("3");
});
//这里,解除绑定失败了,func1函数依然会执行,说明没有去掉
$("#btn-close").click(function(){
$(window).off("resize",func1);
});
});
</script>
$(function(){
//先把所有的函数都绑定进去
$(window).on("resize",function(){
func1("1");
func2("2");
func3("3");
});
//为了要删除其中一个方法:func1,那么我要先把整个resize事件的所有方法都删除掉,然后再重新把需要的绑定进去
$("#btn-close").click(function(){
$(window).off("resize");
$(window).on("resize",function(){
func2("2");
func3("3");
});
});
});
$(window).on("resize",func1("1"),func2("2"),func3("3"));
$(window).off("resize",func1);
var $test = $( "#test" );
function handler1() {
console.log( "handler1" );
$test.off( "click", handler2 );
}
function handler2() {
console.log( "handler2" );
}
$test.on( "click", handler1 );
$test.on( "click", handler2 );
In the code above, handler2 will be executed anyway the first time even if it's removed using .off(). However, the handler will not be executed the following times the click event is triggered.