如何使所选菜单保持选中jquery,css,html

时间:2017-04-02 14:27:34

标签: javascript jquery html css

手风琴菜单,点击js后必须使子菜单保持选中状态。或者css。

不是html中的静态解决方案。 需要包含jquery和css的解决方案 它应该与点击后的悬停相同

查看小提琴

https://jsfiddle.net/shaswatatripathy/ucgff65k/



$(document).ready(function() {

  $("#accordion > li > div").click(function() {
    $("#submenu li").slideUp();
    if (!$(this).next().is(":visible")) {
      $(this).next().slideDown();
    }
  });
});

.accordion {
  width: 100%;
  max-width: 260px;
  background: #FFF;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.accordion .link {
  cursor: pointer;
  display: block;
  padding: 15px 15px 15px 42px;
  font-size: 14px;
  font-weight: 700;
  border-top: 1px solid #CCC;
  border-bottom: 1px solid #CCC;
  border-right: 1px solid #CCC;
  position: relative;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
}


/*-------------Submenu-----------------------------*/

.submenu {
  padding: 0px;
  display: none;
  font-size: 14px;
}

.submenu li {
  border-bottom: 1px solid #4b4a5e;
}

.submenu a {
  display: block;
  text-decoration: none;
  color: #23222d;
  background-color: #CCC;
  padding: 12px;
  padding-left: 42px;
  -webkit-transition: all 0.25s ease;
  -o-transition: all 0.25s ease;
  transition: all 0.25s ease;
}

#submenu li.active {
  display: block;
}

.submenu a:hover {
  background: #b63b4d;
  color: #FFF;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul id="accordion" class="accordion">
  <li>
    <div class="link">Menu 2</div>
    <ul class="submenu">
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
    </ul>
  </li>
  <li>
    <div class="link">Menu 3</div>
    <ul class="submenu">
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

希望这是你正在寻找的,

&#13;
&#13;
$(document).ready(function() {

  $("#accordion > li > div").click(function() {
    $("#submenu li").slideUp();
    if (!$(this).next().is(":visible")) {
      $(this).next().slideDown();
    }
  });
  
  $(".submenu li a").click(function(){
    $(".submenu li a.active").removeClass("active");
    $(this).addClass("active");
  });
});
&#13;
.accordion {
  width: 100%;
  max-width: 260px;
  background: #FFF;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.accordion .link {
  cursor: pointer;
  display: block;
  padding: 15px 15px 15px 42px;
  font-size: 14px;
  font-weight: 700;
  border-top: 1px solid #CCC;
  border-bottom: 1px solid #CCC;
  border-right: 1px solid #CCC;
  position: relative;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
}


/*-------------Submenu-----------------------------*/

.submenu {
  padding: 0px;
  display: none;
  font-size: 14px;
}

.submenu li {
  border-bottom: 1px solid #4b4a5e;
}

.submenu a {
  display: block;
  text-decoration: none;
  color: #23222d;
  background-color: #CCC;
  padding: 12px;
  padding-left: 42px;
  -webkit-transition: all 0.25s ease;
  -o-transition: all 0.25s ease;
  transition: all 0.25s ease;
}

#submenu li.active {
  display: block;
}

.submenu a:hover {
  background: #b63b4d;
  color: #FFF;
}
.submenu li a.active {
  background: #b63b4d;
  color: #FFF;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="accordion" class="accordion">
  <li>
    <div class="link">Menu 2</div>
    <ul class="submenu">
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
    </ul>
  </li>
  <li>
    <div class="link">Menu 3</div>
    <ul class="submenu">
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

.active内的<a>标记创建一个<li>类,该:hover标记与<a> a.active { background: #b63b4d; color: #FFF; } 状态的CSS匹配,如下所示:

 $(document).ready(function() {

   $("#accordion > li > div").click(function() {
     if (!$(this).next().is(":visible")) {
       $(this).next().slideDown();
     } else {
       $(this).next().slideUp();
     }
   });

   $(".submenu a").click(function() {
            $(this).toggleClass("active").parent().siblings().find("a").removeClass("active");
   });

 });

然后只需使用click事件侦听器在单击子菜单时添加和删除此类:

 $(document).ready(function() {
 
   $("#accordion > li > div").click(function() {
     if (!$(this).next().is(":visible")) {
       $(this).next().slideDown();
     } else {
       $(this).next().slideUp();
     }
   });
   
   $(".submenu a").click(function() {
     		$(this).toggleClass("active").parent().siblings().find("a").removeClass("active");
   });
   
 });

我还修改为jQuery,以便在打开状态下单击时可以关闭手风琴。

这是工作的jsfiddle:https://jsfiddle.net/ucgff65k/2/ 和代码段:

.accordion {
  width: 100%;
  max-width: 260px;
  background: #FFF;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.accordion .link {
  cursor: pointer;
  display: block;
  padding: 15px 15px 15px 42px;
  font-size: 14px;
  font-weight: 700;
  border-top: 1px solid #CCC;
  border-bottom: 1px solid #CCC;
  border-right: 1px solid #CCC;
  position: relative;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
}


/*-------------Submenu-----------------------------*/

.submenu {
  padding: 0px;
  display: none;
  font-size: 14px;
}

.submenu li {
  border-bottom: 1px solid #4b4a5e;
}

.submenu a {
  display: block;
  text-decoration: none;
  color: #23222d;
  background-color: #CCC;
  padding: 12px;
  padding-left: 42px;
  -webkit-transition: all 0.25s ease;
  -o-transition: all 0.25s ease;
  transition: all 0.25s ease;
}

.submenu a:hover {
  background: #b63b4d;
  color: #FFF;
}

a.active {
  background: #b63b4d;
  color: #FFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="accordion" class="accordion">
  <li>
    <div class="link">Menu 2</div>
    <ul class="submenu">
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
    </ul>
  </li>
  <li>
    <div class="link">Menu 3</div>
    <ul class="submenu">
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
      <li><a href="#">submenu1</a></li>
    </ul>
  </li>
</ul>
<h1>test</h1>