admin管理员组文章数量:1333383
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
1 Answer
Reset to default 5function 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
本文标签: javascriptSimulate click event on mobile deviceStack Overflow
版权声明:本文标题:javascript - Simulate click event on mobile device - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742335809a2455600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论