如何在iPhone Web应用程序中使用jQuery进行单击事件 [英] How do I use jQuery for click event in iPhone web application
问题描述
当我使用jQuery进行简单的点击事件时,它只适用于链接。有没有办法让它适用于跨度等:
When I use jQuery for a simple click event it only works for links. Is there a way to make it work for spans etc:
$("span.clicked").live("click", function(e){alert("span clicked!")});
$("a.clicked").live("click", function(e){alert("link clicked!")});
SPAN适用于Safari而不适用于Mobile Safari(适用于iPhone或iPad),而A标签适用于两个。
The SPAN works in Safari but not Mobile Safari (on iPhone or iPad) whereas the A tag works in both.
推荐答案
您需要收听touchstart和touchend事件。使用jQuery添加侦听器...
You need to listen for the "touchstart" and "touchend" events. Add the listeners with jQuery...
$('span').bind( "touchstart", function(e){alert('Span Clicked!')} );
您可能希望收听touchstart和touchend,以便您可以验证该元素何时被定位触摸的手指与手指被移除时的目标元素相同。
You may wish to listen for a touchstart and touchend so that you can verify that the element targeted when the finger touched is the same as the element targeted when the finger was removed.
我确信有更好的方法可以做到但是应该工作:))
I'm sure there is probably a better way to do it but that should work :)
编辑:有更好的方法!请参阅 https://stackoverflow.com/a/4910962/16940
There is a better way! See https://stackoverflow.com/a/4910962/16940
这篇关于如何在iPhone Web应用程序中使用jQuery进行单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!