Combining $('body').on('click') with $(window).resize(function() in jQuery

后端 未结 4 439
旧巷少年郎
旧巷少年郎 2021-01-12 08:01

Wondering if there is a way to combine identical code of 2 separate functions into 1 function.

In my case:

jQuery(\'body\').on(\'click\', \'.some_div         


        
4条回答
  •  耶瑟儿~
    2021-01-12 08:52

    create a separate function and call it from required locations:

    jQuery('body').on('click', '.some_div', function(e){
        myFunction();
    });
    
    
    jQuery(window).resize(function() {
        myFunction();
    });
    
    function myFunction(){
       // Long and fancy code
    }
    

提交回复
热议问题