Javascript 在jQuery中,无法解除已添加函数的绑定
我正在尝试向元素添加函数。稍后我将尝试删除该函数。它在jQuery中不起作用Javascript 在jQuery中,无法解除已添加函数的绑定,javascript,jquery,Javascript,Jquery,我正在尝试向元素添加函数。稍后我将尝试删除该函数。它在jQuery中不起作用 var myFunction=function(element){ $(element).animate({ /* Some Code in here */ }); myFunction(element); // I want in a loop } /* No issues with the function */ /* binding this function in anothe
var myFunction=function(element){
$(element).animate({
/* Some Code in here */
});
myFunction(element); // I want in a loop
}
/* No issues with the function */
/* binding this function in another place */
$(otherElement).on('focus', myFunction);
/* In another location I want to off the focus on the element */
$(otherElement).on('focus'); --> This is not working. My expectation is the animation to be stopped.
我也尝试过使用“活/死”和“绑定/取消绑定”,但没有成功。哇,这只是一个参数错误 var myFunction=functionelement{-->您只需要一个dom元素作为参数。但是$otherElement。在'focus',myFunction;,传递给myFunction的是一个jQuery事件对象,而不是dom元素 那么,试试这个:
$(otherElement).on('focus', function(ev/*this is event, not element*/) {
myFunction(this);
});
如果您想解除事件绑定,只需使用$otherElement.off'focus';试试
var myFunction=functionelement{
var$element=$element.animate{
高度:“切换”
}, {
完成:功能{
var flag=$element.data'stopAnimation';
如果旗{
$element.removeData'stopAnimation';
回来
}
myFunctionelement;
}
};
//我想要一个循环
}
$'otherElement'。在'focus'上,函数{
myFunction$'element'
};
$'otherElement'。在'blur'上,函数{
$'element'.stoptrue,true。数据'stopAnimation',true;
};
ElementThank@Screer。这个问题看起来像是附加函数内部引发了太多递归异常。因此,解除绑定对元素不起作用。我想我需要找到另一种方法来解决这个问题。@Siva你认为正确的方法。递归调用动画不好。