如何激活选择并取消选择菜单中的<li>

时间:2016-08-09 06:50:28

标签: jquery html5 twitter-bootstrap css3

我想要点击一个&#34; li&#34; ,&#34;&#34; activeli&#34;&#34;课程已添加到此&#34; li&#34;,但没有奏效。 选中的所有项目,此代码的作用类似于jquery中的toggleclass 请帮我。 我附上了我的代码(html和css)

&#13;
&#13;
$(function () {
            $(".list-group-item").click(function () {


                if ($(this).hasclass('activeli')) {
                    $(this).removeclass('activeli')
                }
                else {
                    $(this).addclass('activeli')
                }
            });
        });
&#13;
.menu {
    height: 500px;
    background-color: rgba(234, 231, 231, 0.81);
}

.menu-search {
    background-color: RGBA(128, 180, 225, 0.69);
    padding: 10px;
}

.menu-header {
    background: linear-gradient(#f6f6f6,#fff,#e7e1e1) !important;
    background-color: #ffffff;
    border-bottom: 1px solid #e7e1e1;
    border-radius: 4px;
    -moz-box-shadow: 0 1px 1px rgba(213, 205, 205, 0.87);
    -webkit-box-shadow: 0 1px 1px rgba(213, 205, 205, 0.87);
    box-shadow: 0 1px 1px rgba(213, 205, 205, 0.87);
    padding: 10px 15px;
}

.menu-title {
    border-top-right-radius: 3px;
    border-top-left-radius: 3px;
}


    .menu-title > a:hover {
        text-decoration: #000 !important;
    }

    .menu-title > span {
        color: #000 !important;
    }

    .menu-title > a {
        text-decoration: none !important;
    }

.position_caret-menu {
    position: absolute;
    left: calc(74% - 177px);
}

.position_caret-submenu {
    position: absolute;
    right: calc(74% - 63px);
    color: #0d95ab;
}
.dashboard {
    height: 46px;
    background-color: #f7f7f7;
    border-bottom: 1px solid #e9e0e0;
    box-shadow: 0px 1px 1px rgba(213, 205, 205, 0.87);
}

    .dashboard > span {
        font-size: 17px;
        color: rgb(34, 110, 183);
        text-align: center;
     
    }

    .dashboard img {
        padding-right: 10px;
        padding-top: 5px;
    }

.show-a span {
    font-size: 17px !important;
    color: #000 !important;
}


.activeli {
    background-color: rgb(153,204,102) !important;
}


a:hover{
    color:#000 !important;
}

a:focus{
    color:#000 !important;
}
&#13;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

     <div class="menu-header">
         <span class="menu-title">
             <a data-toggle="collapse" href="#bissuness" class="show-a">
                 <img src="~/img/brainstorm.png" />
                 <span>bissiness</span>


             </a>
             <i class="fa fa-plus-circle position_caret-menu"></i>
         </span>

     </div>
     <div id="bissuness" class="panel-collapse collapse">
         <ul class="list-group">
             <li class="list-group-item">
                 <a href="#">
                     <img src="~/img/rial-.png" style="padding-left: 5px" />
                     <span>buy</span>


                 </a>
             </li>
             <li class="list-group-item">
                 <a href="#">
                     <img src="~/img/iranian-rial1.png" style="padding-left: 5px" />
                     <span>sale</span>
                 </a>
             </li>

         </ul>

     </div>
&#13;
&#13;
&#13;

非常感谢

1 个答案:

答案 0 :(得分:0)

您使用了错误的关键字&#34; hasClass&#34;用大写字母C不是&#34; hasclass&#34;。 我还修改了菜单的jquery以便正常工作。

&#13;
&#13;
$(function () {
            $(".list-group-item").click(function () {


                if ($(this).hasClass('activeli')) {
                    $(".list-group-item").removeClass('activeli')
                }
                else {
$(".list-group-item").removeClass('activeli')
                    $(this).addClass('activeli')
                }
            });
        });
&#13;
.menu {
    height: 500px;
    background-color: rgba(234, 231, 231, 0.81);
}

.menu-search {
    background-color: RGBA(128, 180, 225, 0.69);
    padding: 10px;
}

.menu-header {
    background: linear-gradient(#f6f6f6,#fff,#e7e1e1) !important;
    background-color: #ffffff;
    border-bottom: 1px solid #e7e1e1;
    border-radius: 4px;
    -moz-box-shadow: 0 1px 1px rgba(213, 205, 205, 0.87);
    -webkit-box-shadow: 0 1px 1px rgba(213, 205, 205, 0.87);
    box-shadow: 0 1px 1px rgba(213, 205, 205, 0.87);
    padding: 10px 15px;
}

.menu-title {
    border-top-right-radius: 3px;
    border-top-left-radius: 3px;
}


    .menu-title > a:hover {
        text-decoration: #000 !important;
    }

    .menu-title > span {
        color: #000 !important;
    }

    .menu-title > a {
        text-decoration: none !important;
    }

.position_caret-menu {
    position: absolute;
    left: calc(74% - 177px);
}

.position_caret-submenu {
    position: absolute;
    right: calc(74% - 63px);
    color: #0d95ab;
}
.dashboard {
    height: 46px;
    background-color: #f7f7f7;
    border-bottom: 1px solid #e9e0e0;
    box-shadow: 0px 1px 1px rgba(213, 205, 205, 0.87);
}

    .dashboard > span {
        font-size: 17px;
        color: rgb(34, 110, 183);
        text-align: center;
     
    }

    .dashboard img {
        padding-right: 10px;
        padding-top: 5px;
    }

.show-a span {
    font-size: 17px !important;
    color: #000 !important;
}


.activeli {
    background-color: rgb(153,204,102) !important;
}


a:hover{
    color:#000 !important;
}

a:focus{
    color:#000 !important;
}
&#13;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

     <div class="menu-header">
         <span class="menu-title">
             <a data-toggle="collapse" href="#bissuness" class="show-a">
                 <img src="~/img/brainstorm.png" />
                 <span>bissiness</span>


             </a>
             <i class="fa fa-plus-circle position_caret-menu"></i>
         </span>

     </div>
     <div id="bissuness" class="panel-collapse collapse">
         <ul class="list-group">
             <li class="list-group-item">
                 <a href="#">
                     <img src="~/img/rial-.png" style="padding-left: 5px" />
                     <span>buy</span>


                 </a>
             </li>
             <li class="list-group-item">
                 <a href="#">
                     <img src="~/img/iranian-rial1.png" style="padding-left: 5px" />
                     <span>sale</span>
                 </a>
             </li>

         </ul>

     </div>
&#13;
&#13;
&#13;