jQuery multiple events to trigger the same function

后端 未结 11 1307
青春惊慌失措
青春惊慌失措 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:29

    I was looking for a way to get the event type when jQuery listens for several events at once, and Google put me here.

    So, for those interested, event.type is my answer :

    $('#element').on('keyup keypress blur change', function(event) {
        alert(event.type); // keyup OR keypress OR blur OR change
    });
    

    More info in the jQuery doc.

提交回复
热议问题