javascript - Simulate click event on mobile device - Stack Overflow

I have a click event on window load like so :function myFunction(){document.getElementById('myEl&#

I have a click event on window load like so :

function myFunction(){
  document.getElementById('myEl').click(); // works on desktop, not on mobile
}

window.onload = myFunction;

As mentioned, this works on desktop but not on mobile devices. I gathered it was down to mobile handling click events differently. Is that correct ?

I need my element to be 'clicked' on load on all devices, desktop and touch pads (mobiles etc).

I have a click event on window load like so :

function myFunction(){
  document.getElementById('myEl').click(); // works on desktop, not on mobile
}

window.onload = myFunction;

As mentioned, this works on desktop but not on mobile devices. I gathered it was down to mobile handling click events differently. Is that correct ?

I need my element to be 'clicked' on load on all devices, desktop and touch pads (mobiles etc).

Share Improve this question asked Nov 15, 2016 at 16:17 thatOneGuythatOneGuy 10.7k9 gold badges58 silver badges92 bronze badges 7
  • 1 Did you try .trigger("click")? – Praveen Kumar Purushothaman Commented Nov 15, 2016 at 16:17
  • @PraveenKumar yeah just implemented that, works on desktop, not on mobile :/ – thatOneGuy Commented Nov 15, 2016 at 16:20
  • @PraveenKumar is window.onload the correct way to start a function on load on mobile devices too do you know ? – thatOneGuy Commented Nov 15, 2016 at 16:28
  • Is it OK if you get the solution using jQuery ? – AsgarAli Commented Nov 15, 2016 at 16:38
  • @thatOneGuy That's right... Nothing wrong in it. – Praveen Kumar Purushothaman Commented Nov 15, 2016 at 16:39
 |  Show 2 more ments

1 Answer 1

Reset to default 5
function myFunction() {
    $("#myEl").trigger('click');
    $("#myEl").trigger('touchstart');
}
window.onload = myFunction;

For touch event, visit below link.

https://developer.mozilla/en-US/docs/Web/Events/touchstart

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742335809a4424625.html

相关推荐

  • javascript - Simulate click event on mobile device - Stack Overflow

    I have a click event on window load like so :function myFunction(){document.getElementById('myEl&#

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信