Reset jQuery one() so that it will fire again

后端 未结 2 634
轻奢々
轻奢々 2020-12-11 06:51

I\'m using the jQuery one() method on an event handler to fire once then de-register itself. Something like:

$(\".main\").one(\'mousedown\', \'.         


        
相关标签:
2条回答
  • 2020-12-11 07:26

    just wrap that code in a function

    function mainMouseDownOne() {
        $(".main").one('mousedown', '.resizeBar', function (e) { /* my code */ });
    }
    
    $(document).ready(function(){
        mainMouseDownOne();
    
        .ajax({
            ...
            success: function() {
                ...
                mainMouseDownOne();
            }
        })
    });
    
    0 讨论(0)
  • 2020-12-11 07:42

    I would recommend wrapping the one() method bind inside a function call, then you can call the function (to bind the initial event handlers) on page load, and again on success of the Ajax call.

    var bindEvents = function(){
      $(".main").one('mousedown', '.resizeBar', function (e) { /* my code */ });
    };
    
    $(function(){ bindEvents(); }); // call the one() method on page load.
    
    $.post(pageURL, function(data){
      bindEvents(); // As an example, after some sort of Ajax or post-reset event.
    });
    
    0 讨论(0)
提交回复
热议问题