jQuery trigger click mobile Safari (iPad)

自古美人都是妖i 提交于 2020-01-03 11:16:49

问题


I have stumbled upon a bug in Safari on iPad.

$('#next_proj a').trigger('click');

.. does not seems to click on the actual link.

Any clues?


回答1:


It may not be a bug. My guess is that they did not want to allow javascript emulated user clicks.




回答2:


I got this to work by doing this...

var el = $('#next_proj a').get(0);
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(evt);

Hope it helps...




回答3:


Have you tried triggering a touch event instead of a click event? Not sure how you would implement in jquery - but it isn't too complicated in plain js

function simulateEvent() {
  var e = document.createEvent('HTMLEvents');
  e.initEvent('touchstart',true, true);
  document.dispatchEvent(e);
}


来源:https://stackoverflow.com/questions/3543733/jquery-trigger-click-mobile-safari-ipad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!