适用于触摸设备的 jQuery 下拉导航。单击另一个菜单项时无法隐藏菜单项
我正在使用 jQuery 为触摸设备创建下拉导航。我已经能够在点击菜单项标题和点击导航外部时隐藏下拉菜单,但是,在点击另一个下拉菜单项标题时我无法隐藏下拉菜单。两者都保持打开状态,直到我单击导航外部或再次单击菜单项标题。
这是脚本:
<script type="text/javascript">
$(document).ready(function(){
$(".toggle_container").addClass("hidden");
$("li#more").click(function(){
if ($(this).children("ul.toggle_container").hasClass("hidden"))
$("ul.toggle_container").toggleClass("hidden");
else{
$("ul.toggle_container").toggleClass("hidden");
}
return false;
})
$("li#search").click(function(){
if ($(this).children("div.toggle_container").hasClass("hidden"))
$("div.toggle_container").toggleClass("hidden");
else{
$("div.toggle_container").toggleClass("hidden");
}
return false;
});
$("li.trigger").click(function(){
$(this).toggleClass("clicked");
});
$(".toggle_container").click(function(e) {
e.stopPropagation();
});
$(document).click(function() {
$(".toggle_container").addClass("hidden");
$("li.trigger").removeClass("clicked");
});
});
</script>
这是标记:
<nav id="navbar">
<ul>
<li id="home"><a href="/mobile/index.html"> </a></li>
<li><a href="#">Item 1</a></li>
<li id="more" class="trigger"><a href="#">More</a>
<ul class="toggle_container">
<li><a href="#">subitem1</a></li>
<li><a href="#">subitem2</a></li>
<li><a href="#">subitem3</a></li>
<li><a href="#">subitem4</a></li>
<li><a href="#">subitem5</a></li>
<li><a href="#">subitem6</a></li>
</ul>
</li>
<li id="search" class="trigger"><a href="#"> </a>
<div id="nav-search-box" class="toggle_container">
<div class="search_box">
<form class="search_form" name=gs action="http://www.google.com">
<input id=q maxlength=256 title="Enter search query" alt=Query name=q>
<input type=hidden name=btnG id=gs value=Search class=bt>
<input type=hidden name=btnG.x value=0>
<input type=hidden name=btnG.y value=0>
<input type=hidden name=oe value=UTF-8>
<input type=hidden name=ie value=UTF-8>
<input type=hidden name=entqr value=3>
<input type=hidden name=ud value=1>
<input type=hidden name=sort value=date:D:L:d1>
<input type=hidden name=output value=xml_no_dtd>
<input type=hidden name=lr value=lang_en>
<input type=hidden name=client value=google>
<input type=hidden name=proxystylesheet value=google>
<input type=hidden name=x value=0>
<input type=hidden name=y value=0>
<input type=hidden name=proxyreload value=1>
<input type=hidden name=entsp value=0>
<input type=hidden name=site value=google>
</form>
</div>
</div>
</li>
</ul>
</nav>
我也愿意接受有关如何改进脚本的任何建议。感谢您的关注。
I'm creating a drop-down nav for touch devices with jQuery. I've been able to hide drop downs when tapping menu item title and when tapping outside the nav, however, I cannot hide a drop-down when tapping on another drop-down menu item title. Both stay open until I click outside the nav or click the menu item titles again.
Here's the script:
<script type="text/javascript">
$(document).ready(function(){
$(".toggle_container").addClass("hidden");
$("li#more").click(function(){
if ($(this).children("ul.toggle_container").hasClass("hidden"))
$("ul.toggle_container").toggleClass("hidden");
else{
$("ul.toggle_container").toggleClass("hidden");
}
return false;
})
$("li#search").click(function(){
if ($(this).children("div.toggle_container").hasClass("hidden"))
$("div.toggle_container").toggleClass("hidden");
else{
$("div.toggle_container").toggleClass("hidden");
}
return false;
});
$("li.trigger").click(function(){
$(this).toggleClass("clicked");
});
$(".toggle_container").click(function(e) {
e.stopPropagation();
});
$(document).click(function() {
$(".toggle_container").addClass("hidden");
$("li.trigger").removeClass("clicked");
});
});
</script>
And here's the markup:
<nav id="navbar">
<ul>
<li id="home"><a href="/mobile/index.html"> </a></li>
<li><a href="#">Item 1</a></li>
<li id="more" class="trigger"><a href="#">More</a>
<ul class="toggle_container">
<li><a href="#">subitem1</a></li>
<li><a href="#">subitem2</a></li>
<li><a href="#">subitem3</a></li>
<li><a href="#">subitem4</a></li>
<li><a href="#">subitem5</a></li>
<li><a href="#">subitem6</a></li>
</ul>
</li>
<li id="search" class="trigger"><a href="#"> </a>
<div id="nav-search-box" class="toggle_container">
<div class="search_box">
<form class="search_form" name=gs action="http://www.google.com">
<input id=q maxlength=256 title="Enter search query" alt=Query name=q>
<input type=hidden name=btnG id=gs value=Search class=bt>
<input type=hidden name=btnG.x value=0>
<input type=hidden name=btnG.y value=0>
<input type=hidden name=oe value=UTF-8>
<input type=hidden name=ie value=UTF-8>
<input type=hidden name=entqr value=3>
<input type=hidden name=ud value=1>
<input type=hidden name=sort value=date:D:L:d1>
<input type=hidden name=output value=xml_no_dtd>
<input type=hidden name=lr value=lang_en>
<input type=hidden name=client value=google>
<input type=hidden name=proxystylesheet value=google>
<input type=hidden name=x value=0>
<input type=hidden name=y value=0>
<input type=hidden name=proxyreload value=1>
<input type=hidden name=entsp value=0>
<input type=hidden name=site value=google>
</form>
</div>
</div>
</li>
</ul>
</nav>
I'm also open to any suggestions about how to improve the script. Thanks for looking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试进行此修改:
这应该与单击文档时的行为相同,但保持单击的 li 处于启用状态。然后,我将删除所有为
toggle_container
分配“隐藏”类的内容,并使用 CSS 来控制它。例如,如果“隐藏”类仅设置display:none
我将拥有如下 CSS:希望有帮助!
Try this modification:
This should then act the same way as when you click onto the document but keep the clicked li enabled. I would then remove all the stuff assigning a 'hidden' class to the
toggle_container
and instead use CSS to control this. For instance, if the 'hidden' class just setsdisplay:none
I would have CSS like so:Hope that helps!