简体   繁体   English

iPhone:跨度点击无法正常工作

[英]iPhone: span click doesn't work fine

I have problem when I want to touch on span 'arrow' to drop down second level of menu navigation. 我想触摸跨度“箭头”以下拉菜单导航的第二级时遇到问题。 When I touch on arrow, result is same as I touch on page (eg Philosophie). 当我触摸箭头时,结果与我在页面上触摸的结果相同(例如Philosophie)。 Arrow have css--> cursor:pointer; 箭头有css-> cursor:pointer; On desktop version it works fine. 在桌面版本上,它可以正常工作。

I use iPhone 5. If I touch the arrow and hold it for 1 second, then it works fine. 我使用iPhone5。如果我触摸箭头并按住1秒钟,则它可以正常工作。 But if I touch it just once, then it leads me to a page. 但是,如果我只触摸一次,那么它将引导我到页面。

http://www.dodaj.rs/f/2n/ym/WnUC5W0/menu.jpg http://www.dodaj.rs/f/2n/ym/WnUC5W0/menu.jpg

 //First js for adding arrow $(".menu-item-has-children").each(function() { $(this).prepend('<span onClick="" class="arrow"></span>'); }); $(".main-menu .menu li").first().addClass("first-page-item"); //Second js for dropwdown submenu $('.arrow').on("click",function(e) { if ($(this).hasClass('opened')) { $(this).removeClass('opened'); $(this).parent().find('.sub-menu').slideToggle(); } else { $(this).addClass('opened'); $(this).parent().find('.sub-menu').slideToggle(); } e.stopPropagation(); }); 
 <ul class="menu"> <li class="menu-item"> <span onclick="" class="arrow opened"></span> <a href="#">Philosophie</a> <ul class="sub-menu" style="width: 208px; display: block;"> <li class="menu-item"> <a href="#">Über uns</a> </li> <li class="menu-item"> <a href="#">Unser weg</a> </li> </ul> </li> <li class="menu-item"></li> <li class="menu-item"></li> </ul> 

Since that iPhone is a touch device, you should use touch events instead of click events. 由于该iPhone是触摸设备,因此应使用触摸事件而不是单击事件。

So, you should: 因此,您应该:

  1. Detect if the device is a mobile 检测设备是否为移动设备
  2. Behave properly depending on the device type 根据设备类型正确运行

Here is the code: 这是代码:

function goArrow(e) {
    if ($(this).hasClass('opened')) {
        $(this).removeClass('opened');
        $(this).parent().find('.sub-menu').slideToggle();
    } else  {
        $(this).addClass('opened');
        $(this).parent().find('.sub-menu').slideToggle();
    }
    e.stopPropagation();
}

var TOUCH = "ontouchstart" in window;

if (TOUCH) $('.arrow').on("touchstart", goArrow);
else $('.arrow').on("click", goArrow);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM