我为打开和关闭子菜单编写此函数,它工作正常,但我有这个问题: 如果用户单击子菜单项并打开页面(此菜单位于母版页中),则子菜单不会关闭。我怎么能这样做?
fiddler Link (more clear with css)
$(document).ready(function () {
$('.has-sub > a').click(function (e) {
e.preventDefault();
var submenu = $(this).next();
if (submenu.is(":visible")) {
submenu.slideUp();
$(this).closest("li").removeClass("active");
}
else {
submenu.slideDown();
$(this).closest("li").addClass("active");
}
});
});
///my html menu it is in my masterpage(i use mvc4 razor)
<div id='cssmenu'>
<ul>
<li class="has-sub"><a href='#'><span>submenu</span></a>
<ul class="subbg">
<li><a href="#"><span>aaaa</span></a></li>
<li><a href="#"><span>bbbb</span></a></li>
<li><a href="#"><span>cccc</span></a></li>
<li><a href="#"><span>ddddd</span></a></li>
</ul>
</li>
<li><a href="#"><span>wwww</span></a></li>
</ul>
</div>