jQuery 手风琴菜单
这只是一个关于 jQuery 的新手问题,但我只是用它来创建一个简单的手风琴菜单。 HTML 输出如下:
<ul id="nav-sub">
<li class="sub-level-0"><a href="#">Menu Item One</a></li>
<li class="parent-here last sub-level-0"><a href="#">Menu Item Two</a>
<ul>
<li class="here sub-level-1"><a href="#">Sub Menu Item One</a></li>
<li class="last sub-level-1"><a href="#">Sub Menu Item Two</a></li>
</ul>
</li>
我目前拥有的 jQuery 是:
$(document).ready(function() {
// Show the children of the first product on page load but leave the others hidden
$("ul#nav-sub li.parent-here ul").show();
// Then attach a visibility toggle to each of the parents
if ( $("ul#nav-sub li.sub-level-0 ul").size > 0 ) {
$("ul#nav-sub li.sub-level-0 > a").click(function(){
$(this).next().slideToggle("slow");
return false; //Prevent the browser jump to the link anchor
});
}
});
这是我能让它完全正常工作的最接近的,但唯一不起作用的是切换动画。它不是缓慢的过渡,而是直接跳开。
我基本上想要的是获得缓慢的过渡效果,但如果菜单项有任何子项(子列表 - 如上面的菜单项二所示),也只返回 false(防止跟随链接锚点的默认浏览器操作)。我需要菜单项一返回 true 并直接进入该页面。
谢谢
This is just a really novice question on jQuery but I'm just using it to create a simple accordion menu. The HTML ouput is like this:
<ul id="nav-sub">
<li class="sub-level-0"><a href="#">Menu Item One</a></li>
<li class="parent-here last sub-level-0"><a href="#">Menu Item Two</a>
<ul>
<li class="here sub-level-1"><a href="#">Sub Menu Item One</a></li>
<li class="last sub-level-1"><a href="#">Sub Menu Item Two</a></li>
</ul>
</li>
And the jQuery I currently have is:
$(document).ready(function() {
// Show the children of the first product on page load but leave the others hidden
$("ul#nav-sub li.parent-here ul").show();
// Then attach a visibility toggle to each of the parents
if ( $("ul#nav-sub li.sub-level-0 ul").size > 0 ) {
$("ul#nav-sub li.sub-level-0 > a").click(function(){
$(this).next().slideToggle("slow");
return false; //Prevent the browser jump to the link anchor
});
}
});
This is the closest I can get it to fully working but the only thing that doesn't work is the toggle animation. Instead of a slow transition it simply jumps right open.
What I basically want is to get the slow transition effect but ALSO to only return false (prevent the default browser action of following the link anchor) if the menu item has any children (a sub list - as shown above on Menu Item Two). I need Menu Item One to return true and go straight to that page.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我和你一样,喜欢弄清楚要学习的东西...我以为这会很容易,但天哪,我花了 30 分钟调试这该死的东西..
参见 http://jsfiddle.net/QcBNK/92/
Html:
JQuery :
注释: 我不是专家..后来我注意到子项目元素抛出事件,奇怪的是 find() 返回一个子元素
即使子元素抛出事件!?所以我无法使用 evt.target==this 来测试冒泡事件。子元素事件返回整个父元素! =S所以我投入了一个黑客!也许有更好的方法。
Im like you and like to figure things out to learn...I thought it was going to be easy but jeez I spent 30 mins debugging the damn thing..
See http://jsfiddle.net/QcBNK/92/
Html:
JQuery :
Notes: Im no expert.. I noticed later that the sub items elements throw the event, and oddly enough find() returns a child
<ul>
even if the sub element threw the event!? So I could not useevt.target==this
to test for a bubbled event. The sub elements event returns the entire parent li! =S So I threw in a haack! Maybe there is a better method.HTML:
JQuery:
HTML:
JQuery: