'活动'班级不起作用

时间:2017-06-01 09:17:00

标签: jquery html css

我的css有问题。我试图将活动类设置为在垂直菜单上以粉红色突出显示,但事实并非如此。如果用户没有悬停在它上面,我如何保持点亮。这是我的HTML和CSS。

HTML CODE:

<div id="cssmenu">
  <ul>
     <li class="active"><a href="http://www.local-trends.com/cellphones--parts.html">Shop All</a></li>
 <li><a href="#">Cell Phones & Smartphones</a></li>
 <li><a href="#">Smart Watches</a></li>
 <li><a href="#">Batteries</a></li>
 <li><a href="#">Cases, Covers & Skins</a></li>
 <li><a href="#">Chargers & Cradles</a></li>
 <li><a href="#">Headsets</a></li>
 <li><a href="#">Phone Cards & SIM Cards</a></li>
 <li><a href="#">Cell Phone & Smartphone Parts</a></li>
  </ul>
</div>

CSS代码:

#cssmenu,
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul ul {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 0;
}
#cssmenu ul {
  position: relative;
  z-index: 597;
  float: left;
}
#cssmenu ul li {
  float: left;
  min-height: 1px;
  line-height: 1em;
  vertical-align: middle;
}
#cssmenu ul li.hover,
#cssmenu ul li:hover {
  position: relative;
  z-index: 599;
  cursor: default;
}
#cssmenu ul ul {
  margin-top: 1px;
  visibility: hidden;
  position: absolute;
  top: 1px;
  left: 99%;
  z-index: 598;
  width: 100%;
}
#cssmenu ul ul li {
  float: none;
}
#cssmenu ul ul ul {
  top: 1px;
  left: 99%;
}
#cssmenu ul li:hover > ul {
  visibility: visible;
}
#cssmenu ul li {
  float: none;
}
#cssmenu ul ul li {
  font-weight: normal;
}
/* Custom CSS Styles */
#cssmenu {
  font-family: 'Lato', sans-serif;
  font-size: 18px;
  width: 200px;
}
#cssmenu ul a,
#cssmenu ul a:link,
#cssmenu ul a:visited {
  display: block;
  color: #848889;
  text-decoration: none;
  font-weight: 300;
}
#cssmenu > ul {
  float: none;
}
#cssmenu ul {
  background: #fff;
}
#cssmenu > ul > li {
  border-left: 3px solid #d7d8da;
}
#cssmenu > ul > li > a {
  padding: 10px 20px;
}
#cssmenu > ul > li:hover {
  border-left: 3px solid #ff0040;
}
#cssmenu ul li:hover > a {
  color: #ff0040;
}
#cssmenu > ul > li:hover {
  background: #f6f6f6;
} 

3 个答案:

答案 0 :(得分:1)

CSS中没有.active个类。您应该将它们添加到CSS中。

更改

:此:

#cssmenu > ul > li:hover{
  border-left: 3px solid #ff0040;
}
#cssmenu ul li:hover > a{
  color: #ff0040;
}
#cssmenu > ul > li:hover{
  background: #f6f6f6;
} 

对此:

#cssmenu > ul > li:hover, #cssmenu > ul > li.active {
  border-left: 3px solid #ff0040;
}
#cssmenu ul li:hover > a, #cssmenu ul li.active > a {
  color: #ff0040;
}
#cssmenu > ul > li:hover, #cssmenu > ul > li.active {
  background: #f6f6f6;
} 

答案 1 :(得分:0)

我看不到申请css课程的任何active

li.active{ color: pink; } //Try using this

答案 2 :(得分:0)

添加jQuery插件和css代码

css:

#cssmenu  ul  li.active a{
         background:pink;
}

HTML代码:

    <div id="cssmenu">
     <ul>
     <li class="active"><a href="http://www.local-trends.com/cellphones--parts.html" onclick="myFunction(this)">Shop All</a></li>
     <li><a href="#" onclick="myFunction(this)">Cell Phones & Smartphones</a></li>
     <li><a href="#" onclick="myFunction(this)">Smart Watches</a></li>
     <li><a href="#" onclick="myFunction(this)">Batteries</a></li>
     <li><a href="#" onclick="myFunction(this)">Cases, Covers & Skins</a></li>
     <li><a href="#" onclick="myFunction(this)">Chargers & Cradles</a></li>
     <li><a href="#" onclick="myFunction(this)">Headsets</a></li>
     <li><a href="#" onclick="myFunction(this)">Phone Cards & SIM Cards</a></li>
     <li><a href="#" onclick="myFunction(this)">Cell Phone & Smartphone Parts</a></li>
     </ul>
    </div>

功能代码:

    <script>
    function myFunction(tag) {
       $("#cssmenu ul li.active").removeClass("active"); //remove previous li active class
       $(tag).closest("li").addClass("active");  //set active class to clicked li
       alert($(tag).text());
    }
    </script>