Jquery unbind events bound with one()

后端 未结 3 1075
孤独总比滥情好
孤独总比滥情好 2020-12-18 22:09

Is there any method to unbind an event that has been bound with one()? Sort of like unone()

相关标签:
3条回答
  • 2020-12-18 22:46

    According to a comment on the doc page:

    Note: you cannot unbind a listener created using .one(). If you want to be able to unbind something that has to occur only once but still be able to unbind it before it occurs, use the example provided for unbinding an event after it's called, and bind it using .bind().

    http://api.jquery.com/one/

    0 讨论(0)
  • 2020-12-18 22:46

    You unbind whatever event that is bounded with .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>

    0 讨论(0)
  • 2020-12-18 22:47

    Quote from JQuery.com:

    You cannot unbind a listener created using .one(). If you want to be able to unbind something that has to occur only once but still be able to unbind it before it occurs, you have to use .bind()

    Something like:

    $("#element").on("click",function(event){
        //do stuff here
        $(this).off(event);
    }
    
    0 讨论(0)
提交回复
热议问题