简体   繁体   中英

Simulating touch events with javascript

I've created a button, which when when clicked opens a popup. I'm just triggering that by simulating a click event at the moment, because there is a 3rd party script involved that I can't directly access.

I'm using this code to duplicate events for touch enabled devices:

['click', 'touch'].forEach(function(eventType){
  btn.addEventListener(eventType, function(event){
    targetElem.click();
    event.preventDefault();
  });
});

Can't seem to find a .click() equivalent for .touch()

btn.dispatchEvent(new TouchEvent('touchstart'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM