简体   繁体   English

JavaScript事件触发两次

[英]JavaScript Event Firing Twice

I have a short piece of JS which is supposed to show and hide the secondary navigation of a site. 我有一小段JS,应该显示和隐藏网站的辅助导航。 If a user clicks Menu Item A it shall show, they click it again and it hides and if Menu Item A is open and they click Menu Item B then it closes A and opens B in its place. 如果用户单击菜单项A,则将显示该菜单项 ,他们将再次单击该菜单项并将其隐藏,并且如果菜单项A已打开并且单击菜单项B,则它将关闭A并在其位置打开B。

The first part of this works, I can click a single menu item and it will open and close, if however I click a different menu item whilst one is open then it shall close (as expected), open (as expected) and then close again. 这项工作的第一部分,我可以单击一个菜单项,然后它将打开和关闭,但是如果我在打开一个菜单项的同时单击其他菜单项,则它将关闭(按预期),打开(按预期)然后关闭再次。 It's as if the event is being fired twice. 好像该事件被触发了两次。

Here is my JS snippet. 这是我的JS代码段。

 $(document).ready(function() { $('.NavButton').click(function(event) { handleSecondaryNavigation(event.target.alt); }); }); function handleSecondaryNavigation(MenuItem) { if ($('#ul' + MenuItem).is(':visible')) { // Menu item visible, hide it $('#SecondaryNavigation').animate({ width: 'toggle' }, 350, function() { $('#ul' + MenuItem).hide(); }); } else if ($('#SecondaryNavigation').is(':visible')) { // Clicked different menu item, hide then show (swap) $('#SecondaryNavigation').animate({ width: 'toggle' }, 350, function() { $('#SecondaryNavigation ul').hide(function() { handleSecondaryNavigation(MenuItem); }); }); } else { // Menu not visible, show it $('#ul' + MenuItem).show(function() { $('#SecondaryNavigation').animate({ width: 'toggle' }, 350); }); } } 
 #MainNavigation { background: #F1F1F1; position: fixed; top: 0px; left: 0px; bottom: 0px; } #MainNavigation .NavButton { width: 20px; display: block; padding: 15px 20px; cursor: pointer; } #SecondaryNavigation { position: fixed; top: 0px; left: 60px; bottom: 0px; background: #DADADA; width: 200px; display: none; box-shadow: 2px 0px 5px #686868; } #SecondaryNavigation ul { display: none; margin: 0px 10px; } #SecondaryNavigation ul li { display: block; padding: 10px; border-bottom: 1px solid #CBCBCB; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav id="MainNavigation"> <img class="NavButton" alt="A" /> <img class="NavButton" alt="B" /> </nav> <nav id="SecondaryNavigation"> <ul id="ulA"> <li>Lorem</li> <li>Ipsum</li> </ul> <ul id="ulB"> <li>Lorem</li> <li>Ipsum</li> </ul> </nav> 

Swapping out the handleSecondaryNavigation(MenuItem) bit and hard coding the action installed of recursively also does not work. handleSecondaryNavigation(MenuItem)位并对递归安装的动作进行硬编码也不起作用。

Any ideas? 有任何想法吗? I'm sure it's probably something silly... 我敢肯定这可能很愚蠢...

Thanks 谢谢

I have solved your issue and please find the working fiddle 我已经解决了您的问题,请找到有用的小提琴

This event listener is triggered twice each time for each ul element 每个ul元素每次都触发两次此事件侦听器

$('#SecondaryNavigation ul').hide(function() {
      console.log("hide trug");
        handleSecondaryNavigation(MenuItem);
      });

change this to 更改为

$('#SecondaryNavigation ul').hide(function() {        
  });
  handleSecondaryNavigation(MenuItem);

I have updated the fiddle as well 我也更新了小提琴

Hope this helps 希望这可以帮助

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

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