我在让 javascript 在我的“li”菜单项上设置活动类时遇到了一点问题。
菜单是用 php 构建的,如下所示。
<nav class="categoriesMenu">
<ul class="nav nav-tabs categories">
<?php
$i=0;
foreach($categories as $k=>$v){
$space = array(" ");
$normalizedCategory = str_replace($space, "", $k);
echo "<li id='id$normalizedCategory'>";
if($i==0){
echo "<a href='#'>$k</a>";
}else{
echo "<a href='#'>$k</a>";
}
$i++;
echo "</li>";
} ?>
</ul>
</nav>
我有以下javascript根据点击设置活动类,但我不能让它只在“a”标签上设置“li”上的活动类
$(document).ready(function(){
var selectCategory=function(id){
var ref=id.replace(' ','');
var classSelector=ref.replace(' ','');
$("nav > ul.categories > li").removeClass("active");
$("#"+ref+" > li").addClass("active");
$('nav > ul.subCategories > li').hide();
$('nav > ul.subCategories > li.'+classSelector).show();
}
$('nav > ul.subCategories > li').click(function(){
if(!$(this).children('li').hasClass("active")){
window[$(this).attr('title')]();
}
$('nav > ul.subCategories > li').removeClass("active");
$(this).children().addClass("active")
});
$('nav > ul.categories > li').click(function(){
var reference=$(this).attr('id');
selectCategory(reference);
})
var firstCat=$('nav > ul.subCategories > li:first').attr('class');
selectCategory(firstCat);
})
菜单以这种格式呈现
<div class="navigation">
<nav class="categoriesMenu">
<ul class="nav nav-tabs categories">
<li id='idManagement'>
<a href='#'>Management</a>
</li>
<li id='idReports'>
<a href='#'>Reports</a>
</li>
<li id='idUsers'>
<a href='#'>Users</a>
</li>
<li id='idAdministration'>
<a href='#'>Administration</a>
</li>
</ul>
</nav>
<nav class="subCategoriesMenu">
<ul class="nav nav-pills subCategories">
<li class='idManagement' id='idManagementRequests' title='RequestsInterface'>
<a>Requests</a>
</li>
<li class='idManagement' id='idManagementTeamActivity' title='ActivityInterface'>
<a>Team Activity</a>
</li>
</ul>
</nav>
任何帮助将非常感激。谢谢