gpt4 book ai didi

jquery - 无法在 if 语句中使用 $(this)

转载 作者:行者123 更新时间:2023-12-01 02:40:37 25 4
gpt4 key购买 nike

如何在 if 语句中使用 $(this) 引用 $('nav > .smart-nav > li') 而不是 $( '.nav-秒')

这是代码-

if ($('nav > .smart-nav > li').is('.active')) {
$('.nav-second').addClass('in');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul class="nav smart-nav">
<li><a href=""><i class="fa fa-home"></i> <span>Dashboard</span></a></li>
<li class="active">
<a data-toggle="collapse" aria-expanded="false" class="collapsed">
<i class="fa fa-home"></i><span>Administrator Portal</span><span class="sub-nav-icon"> <i class="fa fa-angle-down"></i> </span>
</a>
<ul id="" class="nav nav-second collapse" aria-expanded="false">
<li><a href="administrator-portal.html">Dashboard</a></li>
<li><a href="activity.html">Customer Report</a></li>
</ul>
</li>
<li>
<a data-toggle="collapse" aria-expanded="false" class="collapsed">
<i class="fa fa-support"></i><span>Support</span><span class="sub-nav-icon"> <i class="fa fa-angle-down"></i> </span>
</a>
<ul id="" class="nav nav-second collapse" aria-expanded="false">
<li><a href="tableStyles.html">Logs</a></li>
</ul>
</li>
</ul>
</nav>

最佳答案

如下所示:-

$('.smart-nav > li.active').children('ul.nav-second').addClass('in');

示例:-

$('.smart-nav > li.active').children('ul.nav-second').addClass('in');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul class="nav smart-nav">
<li><a href=""><i class="fa fa-home"></i> <span>Dashboard</span></a></li>
<li class="active">
<a data-toggle="collapse" aria-expanded="false" class="collapsed">
<i class="fa fa-home"></i><span>Administrator Portal</span><span class="sub-nav-icon"> <i class="fa fa-angle-down"></i> </span>
</a>
<ul id="" class="nav nav-second collapse" aria-expanded="false">
<li><a href="administrator-portal.html">Dashboard</a></li>
<li><a href="activity.html">Customer Report</a></li>
</ul>
</li>
<li>
<a data-toggle="collapse" aria-expanded="false" class="collapsed">
<i class="fa fa-support"></i><span>Support</span><span class="sub-nav-icon"> <i class="fa fa-angle-down"></i> </span>
</a>
<ul id="" class="nav nav-second collapse" aria-expanded="false">
<li><a href="tableStyles.html">Logs</a></li>
</ul>
</li>
</ul>
</nav>

关于jquery - 无法在 if 语句中使用 $(this),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44456709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com