jQuery 手风琴菜单

发布于 2024-09-29 13:02:51 字数 1322 浏览 2 评论 0原文

这只是一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心作怪 2024-10-06 13:02:51

我和你一样,喜欢弄清楚要学习的东西...我以为这会很容易,但天哪,我花了 30 分钟调试这该死的东西..

参见 http://jsfiddle.net/QcBNK/92/

Html:

<ul id="nav-sub">
    <li class="sub-level-0"><a href="#">Menu Item One</a>
    <ul>
        <li class="sub-level-1"><a href="#">Sub Menu Item One</a></li>
        </ul>
      </li>
     <li class="sub-level-0"><a href="#">Menu Item Two</a>   
      <ul>
                <li class="sub-level-1"><a href="#">Sub Menu Item One</a></li>
                <li class="sub-level-1"><a href="#">Sub Menu Item Two</a></li>
      </ul>  
      </li>    
</ul>

JQuery :

$(document).ready(function() {
    //hide all SUB elements
    $(".sub-level-1").hide(); 
    //attach click to top elements only
    $("li.sub-level-0").mousedown(function(evt) {
        //find the sub element and toggle it..
       if(($(evt.target).text().indexOf("Sub"))!=0) {//Bad hack! sry =(
        $(this).find("ul .sub-level-1").slideToggle();
      }
    });

});

注释: 我不是专家..后来我注意到子项目元素抛出事件,奇怪的是 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:

<ul id="nav-sub">
    <li class="sub-level-0"><a href="#">Menu Item One</a>
    <ul>
        <li class="sub-level-1"><a href="#">Sub Menu Item One</a></li>
        </ul>
      </li>
     <li class="sub-level-0"><a href="#">Menu Item Two</a>   
      <ul>
                <li class="sub-level-1"><a href="#">Sub Menu Item One</a></li>
                <li class="sub-level-1"><a href="#">Sub Menu Item Two</a></li>
      </ul>  
      </li>    
</ul>

JQuery :

$(document).ready(function() {
    //hide all SUB elements
    $(".sub-level-1").hide(); 
    //attach click to top elements only
    $("li.sub-level-0").mousedown(function(evt) {
        //find the sub element and toggle it..
       if(($(evt.target).text().indexOf("Sub"))!=0) {//Bad hack! sry =(
        $(this).find("ul .sub-level-1").slideToggle();
      }
    });

});

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 use evt.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.

友欢 2024-10-06 13:02:51

HTML:

<dl>

<dt><a href="#">Menu Item One</a></dt>
<dd>
    <ul> 
        <li><a href="#">Sub Menu Item One</a></li>        
    </ul>
</dd>

<dt><a href="#">Menu Item Two</a></dt>
<dd>
    <ul> 
        <li><a href="#">Sub Menu Item One</a></li> 
        <li><a href="#">Sub Menu Item Two</a></li> 
    </ul>
</dd>

<dt><a href="#">Menu Item Three</a></dt>
<dd>
    <ul> 
        <li><a href='#'>Sub Menu Item One</a></li> 
        <li><a href='#'>Sub Menu Item Two</a></li> 
        <li><a href='#'>Sub Menu Item Three</a></li>            
    </ul>
</dd>

</dl>

JQuery:

<script>
    $("dd").hide();

    $("dt a").click(function(e) {
        $(e.target).parent().next().slideToggle();
    });
</script>

HTML:

<dl>

<dt><a href="#">Menu Item One</a></dt>
<dd>
    <ul> 
        <li><a href="#">Sub Menu Item One</a></li>        
    </ul>
</dd>

<dt><a href="#">Menu Item Two</a></dt>
<dd>
    <ul> 
        <li><a href="#">Sub Menu Item One</a></li> 
        <li><a href="#">Sub Menu Item Two</a></li> 
    </ul>
</dd>

<dt><a href="#">Menu Item Three</a></dt>
<dd>
    <ul> 
        <li><a href='#'>Sub Menu Item One</a></li> 
        <li><a href='#'>Sub Menu Item Two</a></li> 
        <li><a href='#'>Sub Menu Item Three</a></li>            
    </ul>
</dd>

</dl>

JQuery:

<script>
    $("dd").hide();

    $("dt a").click(function(e) {
        $(e.target).parent().next().slideToggle();
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文