默认情况下使用jquery隐藏子菜单,然后单击子菜单显示并隐藏

时间:2012-06-18 05:53:57

标签: jquery

这是我的HTML代码

<ul class="list">
    <li class="pagenav">
    <a title="Government" href="#">Government</a>
        <ul>
            <li class="page_item page-item-14"><a href="#">Code of Ordinances</a></li>
            <li class="page_item page-item-5267"><a href="#">Finance</a>
                <ul class="children">
                    <li class="page_item page-item-16"><a href="#">Fiscal Year</a></li>
                    <li class="page_item page-item-5280"><a href="#">Comprehensive</a></li>
                </ul>
            </li>
            <li class="page_item page-item-5267"><a href="#">Finance year</a>
                <ul class="children">
                    <li class="page_item page-item-16"><a href="#">Year</a></li>
                    <li class="page_item page-item-5280"><a href="#">Comprehensive year</a></li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

默认情况下显示

Government

Code of Ordinances

Finance

Finance year

点击此后的财务展示

Government

Code of Ordinances

Finance

Fiscal Year

Comprehensive

Finance year

我的意思是说我想隐藏财务和财务年度子菜单,默认情况下点击财务和财务年度那个时间子菜单显示&amp;隐藏

2 个答案:

答案 0 :(得分:1)

$('.page_item .children').hide(0);

$('.page_item').click(function(){
  if($('.children').hasClass('vis')){
    $('.children', this).removeClass('vis');
    $('.children', this).hide();       
  }else{
    $('.children', this).addClass('vis');
    $('.children', this).show();
  }
});

答案 1 :(得分:0)

<style>
  li.pageitem ul.children {
  display:none;
}
</style>

<script>
$('li.pageitem').click(function(){
  $(this).find('ul.children').toggle();
});
</script>