子菜单wordpress切换

时间:2014-10-01 07:18:15

标签: javascript jquery wordpress

我想切换只有子菜单的菜单项但是现在发生的事情是我无法找到切换单个菜单项的方法,每当我切换每个具有子菜单或子菜单的父菜单项时打开切换但不是单一的父项更清楚这里是我试图实现的链接

	jQuery(document).ready(function(){
    jQuery('.sidebar_menu ul ul').hide();
    if(jQuery('.menu-item-has-children').length >0)
    {
    	jQuery('.menu-item-has-children').click(
    		function(event) {
    			jQuery(this).addClass('toggled')
    			if(jQuery('.menu-item-has-children').hasClass('toggled'))
    			{
    			jQuery('.sidebar_menu ul ul').toggle();
    			}
    
    		return false;

    	});
    	    }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidebar_menu">
<div class="menu-sidebar-container"><ul id="menu-sidebar" class="menu"><li id="menu-item-35" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-35 toggled"><a href="http://technocia.com/why-choose-us/">Why Choose Us?</a>
<ul class="sub-menu" style="display: none;">
	<li id="menu-item-38" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-38"><a href="http://technocia.com/why-choose-us/">Why Choose Us?</a></li>
</ul>
</li>
<li id="menu-item-36" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-36"><a href="http://technocia.com/terms-and-condition/">Terms and Condition</a>
<ul class="sub-menu" style="display: none;">
	<li id="menu-item-39" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-39"><a href="http://technocia.com/privacy-policy/">Privacy Policy</a></li>
	<li id="menu-item-40" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-40"><a href="http://technocia.com/resume/">Resume</a></li>
</ul>
</li>
<li id="menu-item-41" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-41"><a href="http://technocia.com/blog/">Blog</a></li>
</ul></div></div>

1 个答案:

答案 0 :(得分:0)

JS Fiddle

您需要更改此

 jQuery('.sidebar_menu ul ul').toggle(); // you are toggeling every class who has .sidebar_menu

 jQuery(this).children('ul').toggle(); // you are only selecting its children which is ul