简体   繁体   English

jQuery,如果在内部单击链接 <li> 则父链接第一次在iPhone上不起作用

[英]jquery if link is clicked inside <li> then parent link doesn't work first time on iPhone

This is really a weird behavior and I have spent a huge amount of time but couldn't figure this out. 这确实是一种怪异的行为,我花了大量时间,但无法弄清楚。

The functionality is: 功能是:

There is a div in the root structure which acts as a mobile button when clicked, it opens a navigation div which is a sibling div of the parent of above div this div has <'ul'> and <'li'> each <'li'> contains sub navigation, when clicks on the sub-navigation links, it opens the third level of navigation without leaving a page. 根结构中有一个div,单击该div时它会充当移动按钮,它会打开一个导航div,该导航div是上述div的父级的同级div,该div分别具有<'ul'>和<'li'> li'>包含子导航,当单击子导航链接时,它将打开第三级导航,而无需离开页面。 then when I click on the above button to close the menu, it doesn't work the first time, then when I click it again the second time then it closes the menu. 然后,当我单击上面的按钮以关闭菜单时,它第一次不起作用,然后当我再次单击它时,它关闭了菜单。

HTML: HTML:

<div id="headerArea">
    <div id="header-navigation">mobile menu/close button</div>
</div>

<div id="navigationMenu">
    <ul>
        <li><a>Link1</a></li>
        <li><a>Link2</a></li>
        <li><a>Link3</a></li>
    </ul>
</div>

JQuery: JQuery的:

$('#header-navigation').on('click',function(e){
    // This is a mobile menu when clicks, open the sub navigation and click back again it should close the menu but when clicks on sub navigation, then the first click doesn't work and it does on the second click. 
 });

$('#navigationMenu li a').on('click', function(){
    // when this clicks, the above link doesn't work the first time but it works the second time.
});

I did another experiment, on the page load, I injected another link inside #navigationMenu but outside the <'ul'> and it was the same behavior then I again injected the same link inside "#navigationMenu ul" then the click event of the link works first time. 我做了另一个实验,在页面加载时,我在#navigationMenu内注入了另一个链接,但在<'ul'>之外,这是相同的行为,然后我再次在“ #navigationMenu ul”内注入了相同的链接,然后单击了click事件。链接第一次生效。

This is something to do with then clicks inside the <'li'> for some reason any link outside the <'ul'> doesn't work 这与在某些情况下单击<'li'>内的链接有关,原因是<'ul'>外的任何链接均不起作用

I just fixed that issue by doing the following. 我只是通过执行以下操作解决了该问题。 As I mentioned this is a weird issue and was only happening on iPhone - Safari 正如我提到的那样,这是一个很奇怪的问题,仅在iPhone-Safari上才发生

JQuery: JQuery的:

$('#header-navigation').on('touchstart click',function(e){
    e.preventDefault(); //The reason I added that, with "touchstart", it was firing ghost clicks
});

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

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