简体   繁体   English

在javascript中激活菜单?

[英]Make a menu in javascript with active?

I have this code http://jsfiddle.net/9nd4j/1722/ 我有这个代码http://jsfiddle.net/9nd4j/1722/

$('ul.navi').each(function () {
// For each set of tabs, we want to keep track of
// which tab is active and it's associated content
var $active, $content, $links = $(this).find('a');

// If the location.hash matches one of the links, use that as the active tab.
// If no match is found, use the first link as the initial active tab.
$active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
$active.addClass('active');

$content = $($active[0].hash);

// Hide the remaining content
$links.not($active).each(function () {
$(this.hash).hide();
});

// Bind the click event handler
$(this).on('click', 'a', function(e){
// Make the old tab inactive.
$active.removeClass('active');
$content.hide();

// Update the variables with the new link and content
$active = $(this);
$content = $(this.hash);

// Make the tab active.
$active.addClass('active');
$content.show();

// Prevent the anchor's default click action
e.preventDefault();
});
});

And I wanted to show text, one at a time, to the "Licenciatura" and the "MEstrado" and not the two simultaneously. 我想一次显示一个文本到“ Licenciatura”和“ MEstrado”,而不是两个同时显示。 Another problem is the active class, just intend that features an active item at a time and not one of "Licenciatura" and "Mestrado" 另一个问题是活动类,只打算一次具有一个活动项,而不是“授权”和“ Mestrado”之一

You can do something like this: 您可以执行以下操作:

$('ul.navi').on('click', 'a', function (e) {
    //Change content displayed
    $($("ul.navi a.active")[0].hash).hide();      
    $(this.hash).show();

    //Change active item
    $("ul.navi a.active").removeClass("active");    
    $(this).addClass("active");  

    e.preventDefault();
});

//Hide all content divs except first one
$("ul.navi a").each(function(index){
    if(index != 0)
        $(this.hash).hide();
    else
        $(this).addClass("active");
});

Check it out here: JSFiddle 在这里查看: JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM