I have a problem with a drop down menu that must remain open to the click. After the menu is open, you can click the link inside and the menu item just clicked.
How can I do to remedy the preventDefault
?
Menu HTML:
<nav class="main-menu">
<ul id="" class="menu">
<li>
<a href="http://www.google.com" target="_blank">Menu One</a>
<div class="sub-menu">
<ul>
<li><a href="http://www.google.uk" target="_blank">test test test</a></li>
... More links ...
</ul>
</div>
</li>
... More items ...
</ul>
</nav>
This is a portion of code
$('.main-menu li a').click(function(event) {
event.preventDefault();
$('.main-menu').find('.sub-menu').removeClass('open');
$(this).parent().find('.sub-menu').addClass('open');
});
An example is visible here JSFIDDLE
Just remove
$('.main-menu').find('.sub-menu').removeClass('open');
Get rid of event.preventDefault();
Instead do like this
<a href="#" onclick="return false">
Then give each main menu a class name. And call the click event on that class.
You can replace your selector with a more targeted ( .menu > li > a
) :
$('.menu > li > a').click(function(event) {
event.preventDefault();
$('.sub-menu.open').removeClass('open');
$(this).parent().find('.sub-menu').addClass('open');
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.