javascript - How to use jquery to check whether class="active" exists in the subordinate li of li?
过去多啦不再A梦
过去多啦不再A梦 2017-06-26 10:54:05
0
3
708
<ul class="nav sidebar-menu">
    <li >
        <a href="/admin/index">
            <i class="menu-icon glyphicon glyphicon-home"></i>
            <span class="menu-text"> 后台首页 </span>
        </a>
    </li>
    <li>
        <a href="#" class="menu-dropdown">
            <i class="menu-icon fa fa-inbox"></i>
            <span class="menu-text"> 产品 </span>
            <i class="menu-expand"></i>
        </a>
        <ul class="submenu">
            <li >
                <a href="/admin/product/lst">
                    <span class="menu-text">产品列表</span>
                </a>
            </li>
            <li >
                <a href="/admin/size/lst">
                    <span class="menu-text">码数管理</span>
                </a>
            </li>
            <li >
                <a href="/admin/cate/lst">
                    <span class="menu-text">产品分类</span>
                </a>
            </li>
        </ul>
    </li>
    <li> //这个li下面的li里面存在active这个class,给这个li增加class="open"
        <a href="#" class="menu-dropdown">
            <i class="menu-icon fa fa-user"></i>
            <span class="menu-text"> 管理员 </span>
            <i class="menu-expand"></i>
        </a>
        <ul class="submenu">
            <li >
                <a href="/admin/user/lst">
                    <span class="menu-text">管理员列表</span>
                </a>
            </li>
            <li class="active"> //这个li里面包含了active这个class,我想给上级li增加一个class="open",应该怎么写才对?
                <a href="/admin/authrule/lst">
                    <span class="menu-text">权限列表</span>
                </a>
            </li>
            <li >
                <a href="/admin/authgroup/lst">
                    <span class="menu-text">用户组</span>
                </a>
            </li>
         </ul>
    </li>
    <li>
        <a href="#" class="menu-dropdown">
            <i class="menu-icon fa fa-gear"></i>
            <span class="menu-text"> 系统设置 </span>
            <i class="menu-expand"></i>
        </a>
        <ul class="submenu">
            <li >
                <a href="/admin/conf/lst">
                    <span class="menu-text">配置列表</span>
                </a>
            </li>
            <li >
                <a href="/admin/conf/conf">
                    <span class="menu-text">配置项</span>
                </a>
            </li>
         </ul>
    </li>
</ul>

I don’t know how to express it, so I wrote the question in the code above. Please forgive me.

过去多啦不再A梦
过去多啦不再A梦

reply all(3)
为情所困

If the level is not deep, you can use parents to get it, such as:

var $menu = $('.sidebar-menu');
var $itemActive = $menu.find('.active'); // 选中项
var $itemActiveParent = $itemActive.parents('li:first'); // 父级
$itemActiveParent.addClass('open');
某草草

What operation do you want to do? .
Give you some code

$(".nav>li").each(function(index){
        $(this).find(".submenu>li").each(function(index1){
            if($(this).hasClass("active")){
                console.log(index,index1);
            }
        })
    })
伊谢尔伦

If you just want to check, you can

var $li = targetLi;    // 你想判断的某个 li
if ($li.find(">ul>li.active").length) {
    // li 的直接下级是 ul,所以它最近的一个下级 li 是 `>ul>li`
    // todo 这里是找到有 .active 的 li
}

If you just want to find all parents of a selected li

$("li.active").parents();

If it is li in the parent

$("li.active").parents("li");

If there is more than one active, you can first use filter or add a condition to find the one you need.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template