adding multiple event listeners to one element

后端 未结 14 1470
夕颜
夕颜 2020-11-27 13:12

So my dilemma is that I don\'t want to write the same code twice. Once for the click event and another for the touchstart event.

Here is the original co

14条回答
  •  无人及你
    2020-11-27 13:34

    Unless your do_something function actually does something with any given arguments, you can just pass it as the event handler.

    var first = document.getElementById('first');
    first.addEventListener('touchstart', do_something, false);
    first.addEventListener('click', do_something, false);
    

提交回复
热议问题