简体   繁体   English

Click事件仅在移动设备上第二次有效,但在台式机上很好

[英]Click event only works second time on mobile, but fine on desktop

I've researched this and tried things like adding e.preventDefault() and return false , but no luck. 我对此进行了研究,并尝试了添加e.preventDefault()return false事情,但是没有运气。 The pulldown effect only happens on the second tap, although it happens on the first click for desktop. 下拉效果仅在第二次点击时发生,尽管它在桌面的第一次点击中发生。

$('.menu-item-has-children').click(function(e) {
        if ( $(this).hasClass('menu-item-slide-in') ) {
            $('.submenu-expand').removeClass('submenu-expand');
            $('> .sub-menu', this).addClass('submenu-expand');
        }
    });

It's basically just a mobile navigation menu. 它基本上只是一个移动导航菜单。 When you click a link with sub items, they expand out, and any other expanded menu collapses. 当您单击带有子项的链接时,它们会展开,其他任何展开的菜单也会折叠。

Here's an example of some HTML with the li that has the menu-item-slide-in class. 这是带有li的一些HTML的示例,该li具有menu-item-slide-in类。 It's taken from a WordPress nav. 它取自WordPress导航。

<li id="menu-item-114" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-114 menu-item-slide-in" aria-haspopup="true"><span class="fa fa-chevron-down main-links-color" aria-hidden="true"></span><a href="#">Company</a>

<ul class="sub-menu">
    <li id="menu-item-103" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-103"><a href="http://localhost/jbec/history/">History</a></li>

Next to listening to the click event, also listen to the touchend event. 在侦听click事件旁边,还侦听touchend事件。 The click event might not work like you think it does on touchscreens. 单击事件可能无法像您认为的那样在触摸屏上起作用。 On iOs there's a timeout which has to occur before the click event is fired and on Android sometimes the click event is not used at all to indicate a click. 在iO上,必须在触发click事件之前发生超时,而在Android上,有时根本不使用click事件来表示点击。 Listening to the touchend event will fix your problem. 听touchend事件将解决您的问题。

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

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