jQuery multiple events to trigger the same function

后端 未结 11 1364
青春惊慌失措
青春惊慌失措 2020-11-22 04:45

Is there a way to have keyup, keypress, blur, and change events call the same function in one line or do I have to do the

11条回答
  •  暖寄归人
    2020-11-22 05:37

    You could define the function that you would like to reuse as below:

    var foo = function() {...}
    

    And later you can set however many event listeners you want on your object to trigger that function using on('event') leaving a space in between as shown below:

    $('#selector').on('keyup keypress blur change paste cut', foo);
    

提交回复
热议问题