[英]iPhone/iPad hover event jQuery
I'm trying to use jQuery to attach a listener onto my navigation so that I can show :hover effects on apple touch devices. 我正在尝试使用jQuery将监听器附加到我的导航上,以便我可以显示:悬停效果在Apple触控设备上。
I cant seem to get it too work with what I have so far, has anybody used this before? 我似乎无法将它与迄今为止的工作结合起来,以前是否有人使用过它?
<script>
$(document).ready(function() {
//ipad and iphone fix
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
$("#primary-navigation li a").bind('touchstart', function(){
console.log("touch started");
});
$("#primary-navigation li a").bind('touchend', function(){
console.log("touch ended");
});
}
});
</script>
I've added an alert just before my selectors which seems to work... 我在我的选择器之前添加了一个警报,似乎有效...
First, if you're on jQuery prior to 1.4.3 try to bind events with .live()
instead of .bind()
. 首先,如果您在1.4.3之前使用jQuery,请尝试使用
.live()
而不是.bind()
绑定事件。 This will ensure that you will bind to that event even for future, dynamically-injected new elements responding to the selector match. 这将确保您将绑定到该事件,即使是未来的,动态注入的新元素也会响应选择器匹配。
If you're on jQuery 1.4.3+ but earlier than 1.7 use .delegate()
instead of .bind()
. 如果您使用的是jQuery 1.4.3+但早于1.7,请使用
.delegate()
而不是.bind()
。
If you're on jQuery 1.7+ then use .on()
. 如果您使用的是jQuery 1.7+,请使用
.on()
。
That said, if your test is taking place on iPhone or iPad, use alert()
instead of console.log(), as you hardly could check console
outputs on those devices. 也就是说,如果您的测试是在iPhone或iPad上进行的,请使用
alert()
而不是console.log(),因为您几乎无法检查这些设备上的console
输出。
I can also point out that the markup breakdown you posted is broken, as you have a </cufon
element missing the '>' character. 我还可以指出,您发布的标记细分已损坏,因为您的
</cufon
元素缺少'>'字符。 It seems this could be a copy-paste issue. 这似乎是一个复制粘贴问题。
Finally, I can see you're using touch* events; 最后,我可以看到你正在使用touch *事件; I must assume you are on jQuery Mobile, jQTouch or something?
我必须假设你在jQuery Mobile,jQTouch或其他东西?
Because if you're not on one of those, I strongly doubt that you could register listeners for touch events, as basic jQuery doesn't have a representation for them in its own API. 因为如果你不是其中之一,我强烈怀疑你可以注册触摸事件的监听器,因为基本的jQuery在它自己的API中没有它们的表示。
I've added an alert... 我添加了警报......
$("nav#primary-navigation ul li a").bind('touchstart', function(){
console.log("touch started");
alert('clicked');
});
Which works when clicked so i know im targeting everything correctly, is there now a way to disable the link click on ul li a? 单击时哪个有效,所以我知道我正确定位所有内容,现在是否有办法禁用链接点击ul li a?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.