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

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

    I am trying this and so far it works (but I am only on Android/Phonegap so caveat emptor)

      function filterEvent( ob, ev ) {
          if (ev.type == "touchstart") {
              ob.off('click').on('click', function(e){ e.preventDefault(); });
          }
      }
      $('#keypad').on('touchstart click', '.number, .dot', function(event) {
          filterEvent( $('#keypad'), event );
          console.log( event.type );  // debugging only
               ... finish handling touch events...
      }
    

    I don't like the fact that I am re-binding handlers on every touch, but all things considered touches don't happen very often (in computer time!)

    I have a TON of handlers like the one for '#keypad' so having a simple function that lets me deal with the problem without too much code is why I went this way.

提交回复
热议问题