简体   繁体   English

带有选项卡式导航的jQuery行为异常

[英]jQuery with tabbed navigation not behaving properly

I'm having trouble getting the tabbed navigation I set up with CSS to behave properly. 我在使用CSS设置的选项卡式导航中无法正常运行。 Basically what needs to happen is that when one button is clicked, removes the hidden class from the div that's accessed by clicking the button, and appends it to the other div. 基本上需要发生的是,当单击一个按钮时,从单击该按钮访问的div中删除隐藏的类,然后将其附加到另一个div。

The behavior I see is that the div that is supposed to become unhidden flashes on screen for a moment, then is immediately hidden again. 我看到的行为是本应变为隐藏状态的div在屏幕上闪烁了一会儿,然后立即被再次隐藏。

The code: 编码:

 $('#js-add-trip').on('click', () => { $('#js-trip-list-form').removeClass('hidden'); $('.saved-trips').addClass('hidden'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='nav'> <nav> <ul> <li id='selected js-saved-trips'> <a href=''>Your Saved Trips</a> </li> <li id='js-add-trip'> <a href=''>Add a Trip</a> </li> </ul> </nav> </div> <div id="sub-container"> <form id="js-trip-list-form" class="add-form hidden"> <!--code for form here--> </form> <div class="saved-trips"> <ul class="trip-list js-trip-list"></ul> <!--population of this area handled by jQuery--> </div> 

Edit: on further investigation, this seems to be a problem with no event.preventDefault(). 编辑:进一步调查,这似乎是没有event.preventDefault()的问题。 I'm working on trying to fix it. 我正在努力修复它。

I don't know if it is exactly what you want. 我不知道这是否正是您想要的。 But to buttons (links) you have to add '#' to href attribute. 但是必须在按钮(链接)上添加'#'到href属性。 And one (last) div missed. 最后一个div错过了。

 $('#js-add-trip').on('click', () => { $('#js-trip-list-form').toggleClass('hidden'); $('.saved-trips').toggleClass('hidden'); }); 
 .hidden { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='nav'> <nav> <ul> <li id='selected js-saved-trips'> <a href='#'>Your Saved Trips</a> </li> <li id='js-add-trip'> <a href='#'>Add a Trip</a> </li> </ul> </nav> </div> <div id="sub-container"> <form id="js-trip-list-form" class="add-form hidden"> <!--code for form here--> hidden </form> <div class="saved-trips"> <ul class="trip-list js-trip-list"> <li>visible</li> </ul> <!--population of this area handled by jQuery--> </div> </div> 

You need to cancel the default action of the link when you click it, by calling the preventDefault() method of the passed event. 单击链接时,您需要通过调用传递的事件的preventDefault()方法来取消该链接的默认操作。

$('#js-add-trip').on('click', (e) => {
  e.preventDefault();
  $('#js-trip-list-form').removeClass('hidden');
  $('.saved-trips').addClass('hidden');
});

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

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