单击其中一个项目后无法保持子菜单打开

时间:2015-02-27 21:56:46

标签: jquery html click submenu

我正在使用jquery点击功能在单击菜单项时显示子菜单。我希望子菜单在我单击其中一个项目之后保持打开状态,直到我返回到主菜单中的另一个项目。我尝试了多种解决方案,阅读多篇帖子并购买了一本关于jquery的书而没有找到答案。我怀疑问题是当点击ul中的li时,无法更改封装ul的显示。但这是我最近的代码尝试。 (任何帮助都会非常感激。我即将放弃,只需通过取出显示器显示所有菜单:隐藏在CSS中。但是我想看看这里是否有人可以回答这个问题。)

Pertinent Html:

     <div id="navbar">          

    <div style="margin-top:5em; margin-left:0em;">    
     <ul class = "nav-level1">
        <li class="nosub"><a href="index.php?content=bethhome" title="Go to home page">Home</a></li>
      <li class="parent"> <a href = "#" >About Us</a>   
            <ul class="nav-level2">
                <li class="nav-whoweare"><a href ="index.php?content=whoweare" title="Who We Are">Who We Are</a></li> 
                <li class="nav-whoweare"><a href ="index.php?content=ourmission" title="Our Mission and Values">Our Mission and Values</a></li> 
                <li class="nav-whoweare"><a href ="index.php?content=ourhistory" title="Our History">Our History</a></li> 
                <li class="parent2"><a href = "#">Our Interim Pastors</a>
                   <ul class = "nav-level3">
                     <li class="nav-whoweare"><a href ="index.php?content=ourpastor" title="Rev. Charles Webb">Rev. Charles Webb</a></li> 
                     <li class="nav-whoweare"><a href ="index.php?content=ourpastor2" title="Vicar Daniel Dockery">Vicar Daniel Dockery</a></li> 

                    </ul>
                </li>                        

              </ul> 
            </li>     
          <li class="nosub"><a href="index.php?content=library" title="Our Library">Library</a></li>
          <li class="nosub"><a href="index.php?content=calendary" title="Calendar of Events">Calendar of Events</a></li>
          <li class="nosub"><a href="index.php?content=missions" title="Our Mission Work">Our Mission Work</a></li>
          <li class="nosub"><a href="index.php?content=ouractivities" title="Our Activities, Groups and Events">Our Activities</a></li>    

    </ul>                                                                         
    </div> <br />

我的CSS代码:

#navbar ul.nav-level2, #navbar ul.nav-level3
{
   font-size:.8em;
   display:none;
} 

#navbar ul.nav-level2 li
{
  margin-left:1em;  
}

} 
 #navbar ul.nav-level3 li
{
  margin-left:2em;
}

我的jquery脚本:

  <script type="text/javascript" src="jquery-1.11.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('.parent').click(function(event) {
      $('.nav-level2').show();
    });
});
</script>   

<script type="text/javascript">
$(document).ready(function() {
    $('.nosub').click(function(event) {
       $('.nav-level3').hide();
       $('.nav-level2').hide();
     });
});
</script> 


<script type="text/javascript">
$(document).ready(function() {
    $('.parent2').click(function(event) {
       $('.nav-level3').show();
       $('.nav-level2').show();
     });
});
</script> 

<script type="text/javascript">    //testng script
$(document).ready(function() {
    $('.nav-whoweare').click(function(event) {

        $'.nav-level2'}.show();
     });
});
</script> 

1 个答案:

答案 0 :(得分:0)

你的代码已经填满了拼写错误。请小心..不要忘记创建一个jsfiddle并使用调试器(例如Google Crome的内置调试器)。

此外,hover()更合适

这是您的工作代码:

$(document).ready(function() {
    $('.parent').hover(
        function() {
            $('.nav-level2').show();
        },
        function() {
            $('.nav-level2').hide();
        }
    );
    $('.parent2').hover(
        function() {
            $('.nav-level3').show();
            $('.nav-level2').show();
        },
        function() {
            $('.nav-level3').hide();
            $('.nav-level2').hide();
        }
    );

    $('.nav-whoweare').hover(
        function() {
            $('.nav-level2').show();
        },
        function() {
            $('.nav-level2').hide();
        }
    );
});