2011-07-06 33 views
0

我正在使用HTML4.01嚴格,CSS3和最新的JQuery。如何選擇多個列表中的多個列表中的相似內容?

我有一個頁面三個相同的名單 - 一個作爲一個菜單欄,其他兩個更小,並出現在主網頁內容的兩側。

我想它,以便選擇在一個列表中的任何一個選項時,也選擇了在其他兩個列表相同的選項。

在這種情況下

「選擇」是指階級「active_tab」的[通過.addClass("active_tab")]

列表中的應用:

<ul id="lt-menu" class="shift_menu"> 
    <li class="li_tab">Home</li> 
    <li class="li_tab">News</li> 
    <li class="li_tab">Info</li> 
    <li class="li_tab">Download</li> 
    <li class="li_tab">Contact</li> 
    <li class="li_tab">Shop</li> 
</ul> 

我一直在嘗試使用這些方針的東西 -

$(".tab, .li_tab").click(function() { 
    $(".active_tab").removeClass("active_tab"); 
    $("li".contains(this)).addClass("active_tab"); 
}); 

,但沒有運氣這麼遠。

我很新的JQuery,所以任何幫助都將不勝感激。

回答

0

伊爾試圖與列表3份(我只是更改ID),以及它的工作原理:

$(".tab, .li_tab").click(function() { 
    //Disable current selection 
    $(".active_tab").removeClass("active_tab"); 

    // We get the index of the <li> in the current list 
    // Each <li> has to be to the same index in the other lists 
    var index = $(this).index(); 

    // We add the 'active_tab' class to all <li> with the same index 
    // Assume that all lists have the 'shift_menu' class, of modify the selector 
    $('.shift_menu').find('li:eq(' + index + ')').addClass("active_tab"); 
}); 

希望它爲你工作...

相關問題