jQuery different events on different elements to trigger the same function

前端 未结 3 616
旧巷少年郎
旧巷少年郎 2021-01-21 17:40

What if different elements have to trigger the same function but on different events ?

$(\'#btnNext\').on(\'click\', function() { /*Same thing*/ });

$(\'#txtFie         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-21 17:52

    You included a clue in your question: "trigger the same function" - so simply bind the same function:

    function commonHandler(e) { /* your code */ }
    
    $('#btnNext').on('click', commonHandler);
    
    $('#txtField').on('change blur', commonHandler);
    

提交回复
热议问题