2017-04-20 60 views
0

當我點擊我的導航鏈接顯示子菜單時,點擊到父頁面。如何停止子菜單鏈接點擊並顯示子導航

如果鏈接有一個子菜單,如何在父頁面加載前短暫顯示子菜單,我該如何禁用它。

$(function(){ 

    //Hide all the sub menus 
    $('.sub-menu').hide(); 

    $("li:has(ul)").click(function(){ 
     //Find the child ul and slideToggle 
     $(this).children("ul").slideToggle(); 
    }); 
}); 

<div class="main-nav navbar-right"><ul id="menu-main-navigation" class="main-nav-ul"><li id="menu-item-46" class="dropdown menu-item menu-item-type-post_type menu-item-object-page menu-item-home menu-item-46"><a href="http://www.chris-whiting.co.uk/development/">Home</a></li> 
<li id="menu-item-77" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-77"><a href="http://www.chris-whiting.co.uk/development/about/">About</a></li> 
<li id="menu-item-82" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-78 current_page_item menu-item-has-children menu-item-82"><a href="http://www.chris-whiting.co.uk/development/services/">Services</a> 
<ul class="sub-menu" style="display: none;"> 
    <li id="menu-item-83" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-83"><a href="http://www.chris-whiting.co.uk/development/services/service-one/">Service One</a></li> 
</ul> 
</li> 
<li id="menu-item-67" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-67"><a href="http://www.chris-whiting.co.uk/development/contact/">Contact</a></li> 
</ul></div> 

回答

3

使用preventDefault()

$("li:has(ul)").click(function(e){ // pass the click event to the function 
     e.preventDefault(); // Stop the link from following 
     //Find the child ul and slideToggle 
     $(this).children("ul").slideToggle(); 
    }); 
+0

謝謝!當它讓我接受答案.. – Chris

+1

這將阻止子錨點工作。你可以用'$('li ul a')來修復它,點擊(function(e){e.stopImmediatePropagation();});' –

+0

正如我剛回來問這個問題,你已經回答了我的問題,謝謝您。 – Chris

相關問題