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

后端 未结 4 413
旧巷少年郎
旧巷少年郎 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:58

    You can create a function to handler both the events and pass this function reference to the event handlers.

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

提交回复
热议问题