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

前端 未结 30 3547
抹茶落季
抹茶落季 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:20

    Taking advantage of the fact that a click will always follow a touch event, here is what I did to get rid of the "ghost click" without having to use timeouts or global flags.

    $('#buttonId').on('touchstart click', function(event){
        if ($(this).data("already")) {
            $(this).data("already", false);
            return false;
        } else if (event.type == "touchstart") {
            $(this).data("already", true);
        }
        //your code here
    });
    

    Basically whenever an ontouchstart event fires on the element, a flag a set and then subsequently removed (and ignored), when the click comes.

提交回复
热议问题