简体   繁体   English

jQuery UL带链接-仅影响li子级

[英]jQuery ul with link - affect only li children

I am trying to create a simple mobile drop down menu using a click to display the submenus. 我正在尝试通过单击显示子菜单来创建一个简单的移动下拉菜单。 I would like ONLY the jQuery ul items that have submenu's to be called on a click and then have access to only effect the li below them. 我只想单击具有子菜单的jQuery jQuery ul项目,然后才可以访问仅影响其下面的li。 In short I click on a button and a drop down for that button shows. 简而言之,我单击一个按钮,然后显示该按钮的下拉菜单。

I would like to NOT have to name them with ID's if possible. 如果可能的话,我不想用ID命名。 I want to have it be more universal that can be used by all ul menus. 我希望它能被所有ul菜单使用的通用性。

Here is one of the jQuery scripts I tried. 这是我尝试过的jQuery脚本之一。 Both jQuery lines calling the html and css do not work. 调用html和css的jQuery都无效。

$('nav ul > li > ul > li.active').click(function() {
    alert("It worked! ");
    $('ul > li').show();// This also does not work. 
});

Here is the nav menu. 这是导航菜单。

<nav>
          <ul>
            <li><a href="home">Home</a></li>
            <li><a href="mission">Mission</a></li>
            <li><a href="co-op">Co-op</a> 
              <!-- First Tier Drop Down -->
              <ul>
                <li><a href="hybrid-model">Hybrid Model</a></li>
                <li><a href="classical-education">Classical Education</a></li>
                <li><a href="courses">Courses</a></li>
                <li><a href="tuition">Tuition</a></li>
                <li><a href="uniforms">Uniforms</a></li>
                <li><a href="staff">Staff</a></li>
                <li><a href="documents">Ducuments</a></li>
              </ul>
            </li>
            <li><a href="Catechesis-of-The-Good-Shepherd">Catechesis of The Good Shepherd</a>
              <ul>
                <li><a href="what-is-cgs">What Is CGS?</a></li>
                <li><a href="level-1">Level 1</a></li>
                <li><a href="level-2">Level 2</a></li>
                <li><a href="level-3">Level 3</a></li>
                <li><a href="uniforms">Uniforms</a></li>
                <li><a href="atrium-child">The Atrium Child</a></li>
                <li><a href="atrium-adult">The Atrium Adult</a></li>
              </ul>
            </li>
            <li><a href="about">About Us</a>
                <ul>
                    <li><a href="board-members">Board Members</a></li>
                    <li><a href="Pro-Ecclesia-Sancta-Catholic-Advance-Movement">Pro Ecclesia Sancta /<br>Catholic Advance Movement</a></li>
                </ul>
            </li>
            <li><a href="contact">Contact</a></li>
          </ul>
  </nav>

Here is one solution, I did not uses ID's but classes: 这是一个解决方案,我没有使用ID而是使用类:

$('li.dropdown').click(function() {
    $('.dropdown.dropdown-open').removeClass('dropdown-open');
    $(this).toggleClass('dropdown-open');
});

http://jsfiddle.net/o1t40hn1/ http://jsfiddle.net/o1t40hn1/

I don't know what you tried to achieve with your JS, because you can not click on sth that is hidden. 我不知道您想用JS实现什么,因为您无法单击隐藏的某物。 So you need to click on the li that has a ul. 因此,您需要单击具有ul的li。 You also could do it without the classes I used by doing sth like: 您也可以在没有我使用的类的情况下执行此操作:

$('li').click(function() {
    if ($(this).find('ul')){
       $('nav > ul > li > ul').hide();
       $(this).find('ul').show();
    }      
});

Here we go: http://jsfiddle.net/b82ed0z3/ 我们开始: http : //jsfiddle.net/b82ed0z3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM