2

我需要在 iphone 菜单中模拟悬停或 mouseup/down,我给出了两个示例。

<ul id="main_nav">
    <li><a href="http://google.com">google</a></li>
    <li><a href="http://yahoo.com">yahoo</a></li>
    <li><a href="#">Three</a>
      <ul>
        <li><a href="#">sub One</a></li>
        <li><a href="#">sub Two</a></li>
      </ul>
</li>
<li><a href="#">Four</a></li>
</ul>

//bind the touchstart event to the link element
$('li').live('touchstart touchend', function(e){
    $(this).toggleClass('btn-sign-up-hover');
    //alert('alert');
});

a {
    display:block;
    line-height:30px;

}

li{
    background:#096;
}

.btn-sign-up-hover {
    background: #F3F;
}

这在iphone中不起作用,

但以下是工作

//bind the touchstart event to the link element
$('li').live('touchstart', function(e){
    $(this).css('background','red');
    //alert('alert');
});

$('li').live('touchend', function(e){
    $('li').css('background','#096');
    //alert('alert');
});

问题是我需要使用toggleClass。

小提琴示例http://jsfiddle.net/UcyAb/

4

2 回答 2

7
//bind the touchstart event to the link element
$('li').live('touchstart', function(e){
    $(this).addClass('btn-sign-up-hover');
    //alert('alert');
});

$('li').live('touchend', function(e){
    $('li').removeClass('btn-sign-up-hover');
    //alert('alert');
});

希望这会奏效。

于 2012-10-27T09:52:59.693 回答
0

我一直在使用类似于@Sameera Thilakasiri 的答案的解决方案,但是我遇到了 iPad 触发 2 个单击事件的问题 - 一个用于单击的菜单项,一个用于菜单下方的任何内容(z-index 明智) . 对我有用的解决方案是触发 touchend 事件的轻微延迟。希望这对某人有帮助!

$('.touchHover').bind('touchstart',function(){
     $(this).addClass('hovered');
     }).bind('touchend',function(){
     $t=setTimeout(function(){$(this).removeClass('hovered');},10);
});
于 2013-06-14T18:26:54.293 回答