jquery prevent duplicate function assigned

前端 未结 4 1239
旧巷少年郎
旧巷少年郎 2020-12-29 22:33

If I need to assign a click function dynamically, is there a way to ensure the click function is only assigned once and not duplicated?

this.click(function()         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-29 22:59

    With jQuery .on() you can do something like that:

    //removes all binding to click for the namespace "myNamespace"
    $(document).off('click.myNamespace'); 
    
    $(document).on('click.myNamespace', '.selector', function(event) {...}); 
    
    //this will be also removed (same namespace)
    $(document).on('click.myNamespace', '.anotherSelector', function(event) {...}); 
    

提交回复
热议问题