How to bind 'touchstart' and 'click' events but not respond to both?

前端 未结 30 3526
抹茶落季
抹茶落季 2020-11-22 14:08

I\'m working on a mobile web site that has to work on a variety of devices. The one\'s giving me a headache at the moment are BlackBerry.

We need to support both key

30条回答
  •  佛祖请我去吃肉
    2020-11-22 14:12

    Another implementation for better maintenance. However, this technique will also do event.stopPropagation (). The click is not caught on any other element that clicked for 100ms.

    var clickObject = {
        flag: false,
        isAlreadyClicked: function () {
            var wasClicked = clickObject.flag;
            clickObject.flag = true;
            setTimeout(function () { clickObject.flag = false; }, 100);
            return wasClicked;
        }
    };
    
    $("#myButton").bind("click touchstart", function (event) {
       if (!clickObject.isAlreadyClicked()) {
          ...
       }
    }
    

提交回复
热议问题