开发者

How do I get this basic jQuery Nav to toggle?

I am still fairly new to jQuery so the following may seem pretty basic to many of you.

I am working on an accordion navigation that uses jQuery (but I am not using jQuery UI). The page is here: http://www.rouviere.com/nav/index.html

Here is the HTML:

<div id="subContent">
<ul>
<li><a href="nikon.html">Nikon</a></li>

<li class="cameras"><a href="#" class="drop">Cameras</a>
<ul>
<li><a href="nikon-d3x.html">Nikon D3x</a></li>
<li><a href="nikon-d3s.html">Nikon D3s</a></li>
<li><a href="nikon-d700.html">Nikon D700</a></li>
<li><a href="nikon-d300s.html">Nikon D300s</a></li>      
</ul>
</li>

<li class="lenses"><a href="#" class="drop">Lenses</a>
<ul>
<li><a href="24-70.html">Nikkor 24-70 f2.8</a></li>
<li><a href="80-200.aspx">Nikkor 80-200 f2.8</a></li> 
<li><a href="300.html">Nikkor 300 f2.8</a></li>
<li><a href="50.html">Nikkor 50 f1.4</a></li>
</ul>
</li>

<li class="bags"><a href="#" class="drop">Bags</a>
<ul>
<li><a  href="bag1.html">Small Bag</a></li>
<li><a href="bag2.html">Medium Bag</a></li>
<li><a href="bag3.html">Large Ba</a></li>
</ul>
</li> 
<li><a href="#" class="drop">Memory Cards</a></li>
</ul>
</div>

Here is the jQuery:

<script type="text/javascript">
$(function(){
    $('.interior #subContent > ul > li > a.drop').click开发者_运维技巧(function(){
    $(this).parent().children('ul').toggle("slow");
         return false;
    });

    $(window).ready(function() {
        $('li.cameras ul').hide();
        $('li.lenses ul').hide();
        $('li.bags ul').hide();
    }); 
}); 
</script>

The page displays with the ul li ul all collapsed, however, they don't toggle open when you click on the parent links.

I would appreciate a seasoned eye or two to review this and help me figure out what is missing.

Thanks!


Try this. I created a single class for your main categories (Cameras, Lenses & Bags). Then I tell the button to find the UL in that LI and toggle it.

<div id="subContent">
<ul>
<li><a href="nikon.html">Nikon</a></li>

<li class="main"><a href="#">Cameras</a>
<ul>
<li><a href="nikon-d3x.html">Nikon D3x</a></li>
<li><a href="nikon-d3s.html">Nikon D3s</a></li>
<li><a href="nikon-d700.html">Nikon D700</a></li>
<li><a href="nikon-d300s.html">Nikon D300s</a></li>      
</ul>
</li>

<li class="main"><a href="#">Lenses</a>
<ul>
<li><a href="24-70.html">Nikkor 24-70 f2.8</a></li>
<li><a href="80-200.aspx">Nikkor 80-200 f2.8</a></li> 
<li><a href="300.html">Nikkor 300 f2.8</a></li>
<li><a href="50.html">Nikkor 50 f1.4</a></li>
</ul>
</li>

<li class="main"><a href="#">Bags</a>
<ul>
<li><a href="bag1.html">Small Bag</a></li>
<li><a href="bag2.html">Medium Bag</a></li>
<li><a href="bag3.html">Large Ba</a></li>
</ul>
</li> 
<li><a href="#">Memory Cards</a></li>
</ul>
</div>

jQuery Below:

<script type="text/javascript">
$(function(){
    $('li.main').click(function(){
    $(this).find('ul').toggle("slow");
         return false;
    });

    $(document).ready(function() {
        $('li.main ul').hide();
    }); 
}); 
</script>

This way uses less code and is easier to expand on in the future. Hope it helps!


Shorter way to write it:

$(function() {
    $('#subContent > ul > li')
        .children('a.drop').click(function() {
            $(this).siblings('ul').toggle("slow");
            return false;
        }).end()
        .children('ul').hide();
}); 

$(window).ready() is meaningless. I'm suprised it did anything before.

A more meaningful and expandable way to write it would probably be this:

$(function() {
    $('#subContent > ul > li').each(function() {
        var toggler = $(this).children('a.drop');
        var sublist = $(this).children('ul');

        toggler.click(function() {
            sublist.toggle("slow");
            return false;
        });
        sublist.hide();
    });
});


I think you need to update your code to be

<script type="text/javascript">
$(function(){
    $('#subContent > ul > li > a.drop').click(function(){
    $(this).parent().children('ul').toggle("slow");
         return false;
    });

    $(window).ready(function() {
        $('li.cameras ul').hide();
        $('li.lenses ul').hide();
        $('li.bags ul').hide();
    }); 
}); 
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜