如何在li中添加类,然后在li下点击任何项目

时间:2013-10-27 08:37:40

标签: javascript php jquery

我遇到了一个问题,并且一直试图解决它几个小时但却无法解决。我有一个侧面栏,其格式如下:

<ul class="collapse" id="menu">
    <li class="panel active">// class "active" should be added <a data-target="#dashboard-nav" class="accordion-toggle" data-toggle="collapse" data-parent="#menu" href="#">
            <i class="icon-dashboard"></i> Dashboard <span class="pull-right"><i class="icon-angle-left"></i></span>
        </a>

        <ul id="dashboard-nav" class="collapse in">// class "in" should be added
            <li class=""><a href="#"><i class="icon-angle-right"></i>Default Style</a>
            </li>
            <li class=""><a href="#"><i class="icon-angle-right"></i> Alternative Style</a>
            </li>
        </ul>
    </li>
    <li class="panel"> <a data-target="#registrations-nav" class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#menu" href="#">
            <i class="glyphicon glyphicon-user"></i> Registrations<span class="pull-right"><i class="icon-angle-left"></i></span>
        </a>

        <ul id="registrations-nav" class="collapse">
            <li><a href="#"><i class="icon-plus-sign"></i> Create Registration</a>
            </li>
            <li class="active"><a href="#"><i class="icon-list"></i> List Registration</a>
            </li>
            <li><a href="#"><i class="icon-adn"></i> Manage Registration</a>
            </li>
        </ul>
    </li>
    <li class="panel"> <a data-target="#settings-nav" class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#menu" href="#">
            <i class="icon-wrench"></i> Settings <span class="pull-right"><i class="icon-angle-left"></i> </span>
        </a>

        <ul id="settings-nav" class="collapse in">
            <li class="active"><a href="/ssw/index.php/admin/service/create"><i class="icon-leaf"></i> Services</a>
            </li>
            <li class=""><a href="#"><i class="icon-angle-right"></i> Sites</a>
            </li>
            <li class=""><a href="#"><i class="icon-angle-right"></i> Service Points</a>
            </li>
        </ul>
    </li>
</ul>

点击<li class="panel">的任何一项后,“有效”类应添加为<li class="panel active">,并在'<ul>后面的“in”类中添加 <li class="panel"> ... <ul id="dashboard-nav" class="collapse in"> 如果我无法解释清楚,请告诉我。

提前致谢

2 个答案:

答案 0 :(得分:0)

fiddle DEMO

$('li.panel').click(function () {
    $('li.panel').removeClass('active');//remove active class from all li with class panel
    $('li.panel').children('ul').removeClass('in');//remove in class
    $(this).addClass('active'); // add class active to current li
    $(this).children('ul').addClass('in'); //add in class to the children ul
});

参考

.addClass()

.removeClass()

.children()

this keyword

答案 1 :(得分:0)

我为它制作了一个javascript解决方案,如下所示: -

        window.onload=start;
        function start(){
            var list = document.getElementById("menu").childNodes;
            for(i=0;i<list.length;i++){
                list[i].onclick=clicked;
            }
        }
        function clicked(){
            var input = this;
            input.className = "panel active"
            var child = input.getElementsByTagName("ul")[0];
            child.className = "collaspe in"
        }