如何使用Javascript在html中设置一个在ul li div中激活的类?

时间:2014-11-04 21:50:45

标签: javascript jquery html css

我有一个导航菜单项,其中我有ul li元素,我想在点击导航栏时更改我的li类的颜色等。 我已经为此写下了一些javascript,但它只打开了导航菜单项,但是当我点击子项时,顶部菜单项被关闭,而li项无法设置为活动状态。 这是我的Javascript:

<script type="text/javascript">
          $(document).ready(function () {
              $('#moduleNav .locked').tooltip();
              $('#moduleNav').on('hide', function () {
                  $(this).find('.accordion-group').each(function () {
                      $(this).removeClass('open');
                  });
              });
              $('#moduleNav').on('show', function (e) {
                  $(e.target).siblings('.accordion-group').addClass('open');
              });
              $('#moduleNav').on('show', function (e) {
                  $(e.target).siblings('.accordion-inner').addClass('li.active');
              });
          });
    </script>

这是html:

<div class="accordion-group  ">
 <a class="accordion-toggle " data-toggle="collapse" data-target="#submenuitem" data-parent="#moduleNav" href="javascript:void(0);">Open menu item</a>
                </div>
<div id="submenuitem" class="accordion-body collapse ">
 <div class="accordion-inner">
<ul>
<li class="">
 <a href="#">sub item 1</a>
 </li>
<li class="">
 <a href="#">sub item 2</a>
</li>
 </ul>
 </div>

这里是css:

#moduleNav .accordion-group .accordion-inner ul li {
    border-bottom: 1px solid #3ab553;
    padding: 10px 6px;
    position: relative;
}

    #moduleNav .accordion-group .accordion-inner ul li.active {
        background: #d6efd3;
    }

        #moduleNav .accordion-group .accordion-inner ul li.active:after {
            left: 100%;
            top: 50%;
            border: solid transparent;
            content: " ";
            height: 0;
            width: 0;
            position: absolute;
            pointer-events: none;
            border-left-color: #d6efd3;
            border-width: 10px;
            margin-top: -10px;
        }

1 个答案:

答案 0 :(得分:0)

示例

CSS:

.active{
  background:#00c189l;
}
.inactive{
  background:gray;
}

HTML:      div id =&#34;例子&#34;类=&#34;不活动的&#34; onclick =&#34;更改();&#34;&gt;示例/ div

JAVASCRIPT: 使用Jquery:

function Change(){

   $('#example').removeClass("inactive");
   $('#example').addClass("active");
}

http://jquery.com/download/