2013-03-18 62 views
3

的重載之後給單擊項目活動類我有幾個嵌套&隱藏子導航列表導航/ Subnav列表,如何

<ul class="nav"> 
<li><a href="index.html">Home</a></li> 
<li><a class="profile" href="#">Profile</a> 
    <ul id="profile"> 
     <li><a href="company.html">Company</a></li> 
     <li><a href="structure.html">Structure</a></li> 
     <li><a href="team.html">Team</a></li> 
    </ul> 
</li> 

<li><a class="projects" href="#">Projects</a> 
    <ul id="projects"> 
     <li><a href="chapter.html">Chapter</a></li> 
     <li><a href="pblc-trde.html">Pblc Trde</a></li> 
     <li><a href="globe.html">Globe</a></li> 
     <li><a href="komforte.html">Komforte</a></li> 
    </ul> 
</li>  

我目前使用的一些jQuery的我在網上找到點擊時顯示/隱藏子導航。我試圖完成的是:

  1. 希望清理sub-nab菜單的顯示/隱藏點擊功能。

  2. 當點擊子導航菜單項時,打開的相應頁面需要擴展子導航並給出相應的菜單項活動類,以便讓用戶知道它們在哪個頁面上。

  3. 我希望純粹在JS/jQuery中做到這一點。該網站的安裝將在WordPress。

    $(document).ready(function() { 
    
    $(".profile").click(function() { 
        var X = $(this).attr('id'); 
        if (X == 1) { 
         $("#profile").hide(); 
         $(this).attr('id', '0'); 
        } else { 
         $("#profile").show(); 
         $(this).attr('id', '1'); 
        } 
    
    }); 
    
    //Mouse click on nav 
    $("#profile").mouseup(function() {}); 
    
    
    //Document Click 
    $(document).mouseup(function() { 
        $("#profile").hide(); 
        $(".profile").attr('id', ''); 
    }); 
    
    
    $(".projects").click(function() { 
        var X = $(this).attr('id'); 
        if (X == 1) { 
         $("#projects").hide(); 
         $(this).attr('id', '0'); 
        } else { 
         $("#projects").show(); 
         $(this).attr('id', '1'); 
        } 
    
    }); 
    
    //Mouse click on nav 
    $("#projects").mouseup(function() {}); 
    
    //Document Click 
    $(document).mouseup(function() { 
        $("#projects").hide(); 
        $(".projects").attr('id', ''); 
    }); 
    }); 
    
    window.onload = function() { 
    $("ul#profile li:first").addClass("active"); 
    }; 
    
    $(document).ready(function() { 
        $("ul#profile").show() 
    }); 
    
+0

您可以使用'document.location.href'(您需要對其進行一些操作)和'$('a [href = something]')'的組合來找到鏈接指向當前頁面。 – Dave 2013-03-18 23:36:14

+0

我在回答中給代碼添加了評論,並做了一些調整以改進對多個子菜單級別的處理。 – Whistletoe 2013-03-19 02:04:32

回答

1
$(document).ready(function() 
{ 
    // Get the name of the page. Split the URL at the '/':s and get the last part 
    // with pop(): 
    var pageName = (location.pathname).split('/').pop(); 

    // If we couldn't get a page name, default to index.html: 
    if(pageName == '') 
    { 
     pageName = 'index.html'; 
    } 

    // Hide ul:s that are children of the navigation: 
    $('.nav ul').hide(); 

    // Event handler for clicks on navigation links: 
    $('.nav a').on('click', function() 
    { 
     // Change visibility for the first ul-child of the current li. 
     // $(this) refers to the clicked element. 
     $(this).parent('li').find('ul').first().toggle(); 

     // Hide other sub-menus: 
     $(this).parents('li').siblings('li').children('ul').hide(); 
    }); 

    // Search through all link elements in the nav menu: 
    $('.nav').find('a').each(function(index, value) 
    { 
     // Append a '$' to the pagename to make the match()-function search 
     // from the end of the href value: 
     pageName += '$'; 

     if(value.href.match(pageName)) 
     { 
      // If the pagename matches the href-attribute, then add the 'active' 
      // class to the parent li, and show parent ul:s: 
      $(this).parent('li').addClass('active').parents('ul').show();  
     } 
    }); 
}); 
+0

先生,你真了不起!這種方式正是我所希望的。我會花時間回顧你寫的內容,並試圖分解它並理解它。非常感謝! – 2013-03-19 01:12:08

+0

嗯謝謝:) – Whistletoe 2013-03-19 01:13:16

+0

只有一個小問題,我看到。當您打開任一子導航菜單並單擊以打開其他子導航時,它不會隱藏先前的子導航 – 2013-03-19 01:14:44

0

你可以使用一個Cookie來保存當前打開的菜單中的價值。這將允許在頁面加載和瀏覽器會話之間保存/檢索值。

正如你已經有了jQuery設置,你可以使用jQuery Cookie plugin來簡化事情。

它的代碼非常簡單(更多插件頁面示例)。

$.cookie('open_menu', 'projects'); //Save 'projects' under 'open_menu' 
$.cookie('open_menu') //Returns 'projects' 

只需檢查頁面加載的值,並在單擊其中一個菜單時將其保存。

如果你不想添加額外的插件,這裏有一些關於JavaScript's inbuilt cookie API的文檔。

編輯:我創建了一個JSFiddle爲您提供示例。 Cookie代碼似乎不適用於那裏的沙箱,但代碼應該適合您,讓我知道如果您有任何問題。

$(window).load(function() { 
if ($.cookie('show_menu') !== undefined) { 
    $('#' + $.cookie('show_menu')).click(); 
} 

$('.nav > li > ul').each(function() { 
    //Hide the sub lists 
    $(this).hide(); 
    //Get link with same ID as Class 
    var id = $(this).attr('id'); 
    //When link is clicked 
    $('.' + id).click(function() { 
     //Get the sub list 
     var list = $('#' + $(this).attr('class')); 
     //Check if it's currently visible 
     if (list.is(':visible')) { 
      list.hide(); //Hide list  
      $.cookie('show_menu', ''); //Unset open menu 
     } else { 
      $('.nav > li > ul').hide(); //Hide all other lists 
      list.show(); //Show list 
      $.cookie('show_menu', list.attr('class')); //Set open menu 
     } 
    }); 
}); 
}); 
+0

我還是很新的手工編碼js,所以使用這個js Cookie對我來說很困難。我理解需要發生什麼的概念,但我不知道如何實施。我知道這可能是一個困難/冗長的問題要回答,但坦白地說,我完全卡住了,任何示例都將不勝感激。 – 2013-03-19 00:58:32

+0

@BrandonLeung我已經在JSFiddle中爲你添加了一些示例代碼,它可以用於你的HTML佈局。 – Dracs 2013-03-19 01:24:36