如何在 iPhone Web 应用程序中使用 jQuery 进行点击事件
当我使用 jQuery 进行简单的单击事件时,它仅适用于链接。有没有办法让它适用于跨度等:
$("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 标签适用于两者。
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!")});
The SPAN works in Safari but not Mobile Safari (on iPhone or iPad) whereas the A tag works in both.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我也为此苦苦挣扎。经过大量的尝试并试图找出问题之后,我找到了一个简单的解决方案。
如果您将元素的
cursor
设置为pointer
,它会神奇地再次与Jquery的live和click事件一起工作。这可以在 CSS 中进行全局设置。I struggled with this as well. After lots of toying around and trying to figure out the problem, I came across a simple solution.
If you set the element's
cursor
topointer
, it magically works again with Jquery's live and the click event. This can just be set globally in the CSS.您需要监听“touchstart”和“touchend”事件。使用 jQuery 添加侦听器...
您可能希望侦听 touchstart 和 touchend,以便可以验证手指触摸时的目标元素与手指移开时的目标元素相同。
我确信可能有更好的方法来做到这一点,但应该工作:)
编辑:有更好的方法!请参阅https://stackoverflow.com/a/4910962/16940
You need to listen for the "touchstart" and "touchend" events. Add the listeners with jQuery...
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 :)
Edit: There is a better way! See https://stackoverflow.com/a/4910962/16940
实际上,您不需要使用 touchstart 或 touchend 事件,只要“span”标签(或“a”标签以外的任何内容)具有以下 css 属性:
单击将注册
You actually don't need to use the touchstart or touchend event, so long as the 'span' tag (or anything other than an 'a' tag) has a css property of:
the click will register
您还可以通过添加空的 onclick 属性来诱导浏览器生成单击事件。对于腰带和大括号方法,以防任一方法在任何给定的 iOS 更新中停止工作,您可以使用类似以下的内容:(
假设您没有任何实际的 onclick 属性,您不介意删除)
You can also coax the browser to generate click events by adding an empty onclick attribute. For a belt-and-braces approach in case either approach stops working in any given iOS update, you could use something like this:
(assuming you don't have any actual onclick attributes you don't mind obliterating)
您可以添加一个空的
onclick
属性,如下所示:You can add an empty
onclick
attribute, like so:我尝试了一切方法,但没有一个有效。事实证明,我无法获取点击事件,因为我的 img 下有一个视频元素。视频元素显然会吃掉点击事件。
I tried everything and none of the tricks worked. It turned out I couldn't get click events because I had a video element under my img. video elements apparently eat click events.
当面向 iOS 时,您必须考虑以下事项: 在 jQuery 中进行事件委托,如
$(document).on('click', '.target-element', function (event) {... });
将不起作用。您必须将onclick=""
添加到目标 HTML 元素,或将cursor:pointer
添加到其样式。摘自并改编自 http://gravitydept.com/blog/js- ios 上的点击事件冒泡:
这就是
标记有效而
无效的原因。
When targeting iOS you have to take the following into consideration: doing event delegation in jQuery like
$(document).on('click', '.target-element', function (event) {...});
will not work. You have to add eitheronclick=""
to the target HTML element orcursor: pointer
to its styles.Taken and adapted from http://gravitydept.com/blog/js-click-event-bubbling-on-ios:
That's the reason while the
<a>
tag works while<span>
doesn't.我在 document.click 上解决这个误解的方法。
在标签正文下一个标签之后添加到 html
<代码><正文>;
...
该标签的一些样式
一些 JQuery 人员
// 在文档单击时隐藏覆盖
$('#overlaySection').click(function(){
$(this).removeClass('active');
});
以及激活此叠加层的主要内容
希望这种方法对某人有用。快乐编码!
My approach to solve this misunderstanding on document.click.
Add into html after tag body next tag
<body>
<div id="overlaySection" onclick="void(0)"></div>
...
</body>
Some style for that tag
Some JQuery staff
// hide overlay on document click
$('#overlaySection').click(function(){
$(this).removeClass('active');
});
and the main thing to active this overlay
Hope this approach will be useful to someone. Happy coding!