有没有一种方法可以解除已经绑定了one()的事件?就像unone()一样。
JQuery.com的引用:
使用
.one()
创建的侦听器无法解绑。 如果您想能够解绑仅发生一次但仍然能够在其发生之前解绑它的内容, 您必须使用.bind()
类似于:
$("#element").on("click",function(event){
//do stuff here
$(this).off(event);
}
你可以使用 .one() 方法来解除绑定任何已经绑定的事件。
$('your_element').unbind('event_called_with_one');
$('input[type=text]').focus(function(){
$(this).one('keypress',function(){console.info('This should not print!');});
if($('input[type=checkbox]').prop('checked'))$(this).unbind('keypress');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type=text>
<label><input type=checkbox checked>unbind</label>