Prevent click event from firing when dblclick event fires

前端 未结 12 1258
名媛妹妹
名媛妹妹 2020-12-02 20:17

I\'m handling both the click and dblclick event on a DOM element. Each one carries out a different command, but I find that when double clicking on the element, in addition

12条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 20:56

    You can use UIEvent.detail if you want to detect how many times the element was clicked and fire events based on that.

    A simple example:

    element.addEventListener("click", function (e) {
      if (e.detail === 1) {
        // do something if the element was clicked once.
      } else if (e.detail === 2) {
        // do something else if the element was clicked twice
      }
    });
    

提交回复
热议问题