我正在为我们网站的标题构建一个Mega菜单。我正在使用jQuery toggleClass为“抽屉”设置活动状态。我对jQuery相当新,并没有进入很多更复杂的构造代码的方法,所以我认为我目前的实现是非常繁琐/冗长的,我想知道是否有更好的方法来做到这一点课程切换。请注意(至少在这一点上)我选择不使用悬停路线,而是使用单击切换。悬停菜单可以是PITA。
这是我的CodePen中包含所有内容,包括有问题的jQuery: http://codepen.io/ksherman/pen/YPMXJW
这是jQuery块:
$('#admissions').click(function(){
$(this).parent().toggleClass('active');
$('.admissionsNav').toggleClass('active');
$('.degreesNav, .applyNav, .visitNav, .campusNav, .aboutNav, .searchNav').removeClass('active');
$('#degrees, #apply, #visit, #campus, #about, #search').parent().removeClass('active');
});
$('#degrees').click(function(){
$(this).parent().toggleClass('active');
$('.degreesNav').toggleClass('active');
$('.applyNav, .visitNav, .campusNav, .aboutNav, .searchNav, .admissionsNav').removeClass('active');
$('#admissions, #apply, #visit, #campus, #about, #search').parent().removeClass('active');
});
$('#apply').click(function(){
$(this).parent().toggleClass('active');
$('.applyNav').toggleClass('active');
$('.visitNav, .campusNav, .aboutNav, .searchNav, .admissionsNav, .degreesNav').removeClass('active');
$('#visit, #campus, #about, #search, #admissions, #degrees').parent().removeClass('active');
});
$('#visit').click(function(){
$(this).parent().toggleClass('active');
$('.visitNav').toggleClass('active');
$('.campusNav, .aboutNav, .searchNav, .admissionsNav, .degreesNav, .applyNav').removeClass('active');
$('#admissions, #degrees, #apply, #campus, #about, #search').parent().removeClass('active');
});
$('#campus').click(function(){
$(this).parent().toggleClass('active');
$('.campusNav').toggleClass('active');
$('.aboutNav, .searchNav, .admissionsNav, .degreesNav, .applyNav, .visitNav').removeClass('active');
$('#admissions, #degrees, #apply, #visit, #about, #search').parent().removeClass('active');
});
$('#about').click(function(){
$(this).parent().toggleClass('active');
$('.aboutNav').toggleClass('active');
$('.searchNav, .admissionsNav, .degreesNav, .applyNav, .visitNav, .campusNav').removeClass('active');
$('#admissions, #degrees, #apply, #visit, #campus, #search').parent().removeClass('active');
});
$('#search').click(function(){
$(this).parent().toggleClass('active');
$('.searchNav').toggleClass('active');
$('.admissionsNav, .degreesNav, .applyNav, .visitNav, .campusNav, .aboutNav').removeClass('active');
$('#admissions, #degrees, #apply, #visit, #campus, #about').parent().removeClass('active');
});
$('#body').click( function () {
$('.admissionsNav, .degreesNav, .applyNav, .visitNav, .campusNav, .aboutNav, .searchNav').removeClass('active');
$('#admissions, #degrees, #apply, #visit, #campus, #about, #search').parent().removeClass('active');
});
答案 0 :(得分:2)
将常用元素合并为一个方法:
使用Javascript:
$('#awesome_ul li a').click(function(){
$(this).parent().toggleClass('active');
//hide all
$('.admissionsNav, .degreesNav, .applyNav, .visitNav, .campusNav, .aboutNav, .searchNav').removeClass('active');
$('#admissions, #degrees, #apply, #visit, #campus, #about, #search').parent().removeClass('active');
//show wanted one
$('.'+$(this).attr('id')+'Nav').toggleClass('active');
});
HTML:
<div class="primary">
<div class="row collapse">
<div class="small-12 columns">
<ul id="awesome_ul">
答案 1 :(得分:1)
您可以将它们合并为一个功能,并为所有按钮调用它。
var menus = ['admissions', 'degrees', 'apply', 'visit', 'campus', 'about', 'search'];
function menuClick(e) {
$(this).parent().toggleClass('active');
$('.' + e.target.id + 'Nav').toggleClass('active');
var otherMenus = menus.filter(function (item) { return item !== e.target.id });
otherMenus.forEach(function (menu) {
$('.' + menu + 'Nav').removeClass('active');
$('#' + menu).parent().removeClass('active');
});
}
menus.forEach(function (menu) {
$('#' + menu).click(menuClick);
});
$('#body').click( menuClick );
请参阅this codepen