我有来自数据库的动态php菜单,类似于这个html结构。
如果我点击LED电视或电视类别我想将活动课程设置为所有类别,如下所示:dropdown-item dropdown-toggle active
我怎么能在php或jquery中做到这一点?
<li class="dropdown"><a id="20" class="dropdown-item dropdown-toggle " href="index.php?page=AllCategories&categoriesgroup=allcategoriesgroup"> ALL CATEGORIES<i class="fas fa-caret-down"></i></a>
<ul class="dropdown-menu">
<li class="dropdown-submenu dropdown-reverse"><a id="402" class="dropdown-item" href="index.php?page=productCategories&categories=tvcategories">TV CATEGORIES<i class="fas fa-caret-down"></i></a>
<ul class="dropdown-menu">
<li class=""><a id="403" class="dropdown-item" href="index.php?page=productDetail&product=ledtv">LED TV</a></li>
</ul>
</li>
</ul>
</li>
你可以使用.parents("li").last()
来获得最高分。
$(".dropdown-item").click(function() {
$(this).parents("li").last().toggleClass("active");
});
$(".dropdown-item").click(function() {
$(this).parents("li").last().toggleClass("active");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="dropdown"><a id="20" class="dropdown-item dropdown-toggle " href="#"> ALL CATEGORIES<i class="fas fa-caret-down"></i></a>
<ul class="dropdown-menu">
<li class="dropdown-submenu dropdown-reverse"><a id="402" class="dropdown-item" href="#">TV CATEGORIES<i class="fas fa-caret-down"></i></a>
<ul class="dropdown-menu">
<li class=""><a id="403" class="dropdown-item" href="#">LED TV</a></li>
</ul>
</li>
</ul>
</li>
</ul>
$(document).on('click', '.test', function () {
var newids=$(this).data('id');
$(this).parents().children('#'+newids).addClass("active");
});
.active{
color:red !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="dropdown"><a id="20" class="dropdown-item dropdown-toggle " href="#"> ALL CATEGORIES<i class="fas fa-caret-down"></i></a>
<ul class="dropdown-menu">
<li class="dropdown-submenu dropdown-reverse"><a id="402" class="dropdown-item" href="#">TV CATEGORIES<i class="fas fa-caret-down"></i></a>
<ul class="dropdown-menu">
<li class="test" data-id="20"><a id="403" class="dropdown-item" href="#">LED TV</a></li>
</ul>
</li>
</ul>
</li>
</ul>
$(document).on('click', '.test', function () {
var newids=$(this).data('id');
$(this).parents().children('#'+newids).addClass("active");
});
.active{
color:red !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="dropdown"><a id="20" class="dropdown-item dropdown-toggle " href="#"> ALL CATEGORIES<i class="fas fa-caret-down"></i></a>
<ul class="dropdown-menu">
<li class="dropdown-submenu dropdown-reverse"><a id="402" class="dropdown-item" href="#">TV CATEGORIES<i class="fas fa-caret-down"></i></a>
<ul class="dropdown-menu">
<li class="test" data-id="20"><a id="403" class="dropdown-item" href="#">LED TV</a></li>
</ul>
</li>
</ul>
</li>
</ul>