2010-05-31 44 views
1

上這是種從以前的問題,我發佈的遵循,但我沒有能夠得到它的工作..jQuery的標籤 - 多組上

我試圖用多套的標籤(jQuery)在一個頁面上。

這是代碼我有一組標籤的偉大的工程:

$('div.tabs div.tab').hide(); 
$('div.tabs div:first').show(); 
$('div.tabs ul.htabs li:first a').addClass('current'); 
$('div.tabs ul.htabs li a').click(function(){ 
    $('div.tabs ul.htabs li a').removeClass('current'); 
    $(this).addClass('current'); 
    var currentTab = $(this).attr('href'); 
    $('div.tabs div.tab').hide(); 
    $(currentTab).show(); 
    return false; 
}); 

要使用一組以上我分配#ID對每個選項卡設置和頁面上試圖與以impliment此:

$.each(['#tabs-1', '#tabs-2', '#tabs-3' ], function(id) { 
    $(id + 'div.tab').hide(); 
    $(id + 'div:first').show(); 
    $(id + 'ul.htabs li:first a').addClass('current'); 
    $(id + 'ul.htabs li a').click(function(){ 
     $(id + 'ul.htabs li a').removeClass('current'); 
     $(this).addClass('current'); 
     var currentTab = $(this).attr('href'); 
     $(id + 'div.tab').hide(); 
     $(currentTab).show(); 
     return false; 
    }); 
}); 

顯然我在這裏做錯了,但作爲一個jQuery新手我很難過!

回答

2

好,所以這是不與代碼正確的間距甚至工作,但我已經找到了更輕質的解決方案,它完美的作品 - jQuery的MiniTabs:

/* 
* jQuery MiniTabs 0.1 - http://code.google.com/p/minitabs/ 
*/ 
jQuery.fn.minitabs = function(speed,effect) { 
    var id = "#" + this.attr('id') 
    $(id + ">div:gt(0)").hide(); 
    $(id + ">ul>li>a:first").addClass("current"); 
    $(id + ">ul>li>a").click(
    function(){ 
     $(id + ">ul>li>a").removeClass("current"); 
     $(this).addClass("current"); 
     $(this).blur(); 
     var re = /([_\-\w]+$)/i; 
     var target = $('#' + re.exec(this.href)[1]); 
     var old = $(id + ">div"); 
     switch (effect) { 
     case 'fade': 
      old.fadeOut(speed).fadeOut(speed); 
      target.fadeIn(speed); 
      break; 
     case 'slide': 
      old.slideUp(speed); 
      target.fadeOut(speed).fadeIn(speed); 
      break; 
     default : 
      old.hide(speed); 
      target.show(speed) 
     } 
     return false; 
    } 
); 
} 

使用此代碼,您可以再加:

$('#tabs-1').minitabs(); 
$('#tabs-2').minitabs(); 
$('#tabs-3').minitabs(); 

一切都很完美!

1
$(id + 'div.tab').hide(); 

id和'​​div.tab'之間沒有空格嗎?這將評估爲"#tabs-1div.tab"

+0

啊是的。但是,即使糾正這一點也沒有奏效。 – 2010-05-31 17:23:54