Home >Web Front-end >JS Tutorial >Implement accordion menu based on Jquery code_jquery

Implement accordion menu based on Jquery code_jquery

WBOY
WBOYOriginal
2016-05-16 15:31:121212browse

I will show you the renderings first:

Look at the page code first, the nesting of the list:

<div id="menuDiv">
  <ul id="menu">
    <li class="parentLi">
      <span>B</span>
      <ul class="childrenUl">
        <li class="childrenLi"><span>C</span></li>
        <li class="childrenLi"><span>C</span></li>
        <li class="childrenLi"><span>C</span></li>
      </ul>
    </li>
    <li class="parentLi">
      <span>B</span>
      <ul class="childrenUl">
        <li class="childrenLi"><span>C</span></li>
        <li class="childrenLi"><span>C</span></li>
        <li class="childrenLi"><span>C</span></li>
      </ul>
    </li>
    <li class="parentLi">
      <span>B</span>
      <ul class="childrenUl">
        <li class="childrenLi"><span>C</span></li>
        <li class="childrenLi"><span>C</span></li>
        <li class="childrenLi"><span>C</span></li>
      </ul>
    </li>
  </ul>
</div>

CSS code, mainly sets the background color and the style of the left border of the submenu, sets the floating and selected styles, and starts to set the submenu not to display (display after clicking through js setting):

#menuDiv{
  width: 200px;
  background-color: #029FD4;
}
.parentLi
{
  width: 100%;
  line-height: 40px;
  margin-top: 1px;
  background: #1C73BA;
  color: #fff;
  cursor: pointer;
  font-weight:bolder;
}
.parentLi span
{
  padding: 10px;
}
.parentLi:hover, .selectedParentMenu
{
  background: #0033CC;
}
.childrenUl
{
  background-color: #ffffff;
  display: none;
}
.childrenLi
{
  width: 100%;
  line-height: 30px;
  font-size: .9em;
  margin-top: 1px;
  background: #63B8FF;
  color: #000000;
  padding-left: 15px;
  cursor: pointer;
}
.childrenLi:hover, .selectedChildrenMenu
{
  border-left: 5px #0033CC solid;
  background: #0099CC;
  padding-left: 15px;
}

Next is the code for the click event:

$(".parentLi").click(function(event) {
    $(this).children('.childrenUl').slideToggle();
  });
  $(".childrenLi").click(function(event) {
    event.stopPropagation();
    $(".childrenLi").removeClass('selectedChildrenMenu');
    $(".parentLi").removeClass('selectedParentMenu');
    $(this).parents(".parentLi").addClass('selectedParentMenu');
    $(this).addClass('selectedChildrenMenu');
  });

It should be noted that nested lists will cause events to bubble up, so bubbling must be organized in the click event of the submenu, event.stopPropagation();

The above code is very simple, the code is just a comment, and the jquery accordion menu is implemented. Friends in need can come and refer to it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn