onClick not working on mobile (touch)

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

Ok, what im trying to do is slide a div down when the user clicks a list item.

Problem is I am using Selectric https://github.com/lcdsantos/jQuery-Selectric which converts a Select box to an unordered list. So when the user clicks a which the source outputs as a list item I want a div to slide down.

On mobile safari (iOS7) the selectbox UI is the same as the standard selectbox UI.

What is the best practice when it comes to onClick for mobile devices?

Basic jquery:

$(window).load(function() {         $('.List li').click(function() {             $('.Div').slideDown('500');         });     }); 

You can see a working example HERE (Advanced search on the side bar)

Thanks.

回答1:

better to use touchstart event with .on() jQuery method:

$(window).load(function() { // better to use $(document).ready(function(){     $('.List li').on('click touchstart', function() {         $('.Div').slideDown('500');     }); }); 

And i don't understand why you are using $(window).load() method because it waits for everything on a page to be loaded, this tend to be slow, while you can use $(document).ready() method which does not wait for each element on the page to be loaded first.



回答2:

you can use instead of click :

$('#whatever').on('touchstart click', function(){ /* do something... */ }); 


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