2011-04-11 38 views
0

我有一个页面,几个选项卡。 我正在寻找一种方法,如果您选择选项卡5和cookie = 1,然后显示tab6,否则显示tab5。如果cookie = 1,我如何通过ID淡入div?

的代码的选项卡是:

jQuery(".tabs a").click(function() { 
    stringref = jQuery(this).attr("href").split('#')[1]; 
    jQuery('.tab_content:not(#' + stringref + ')').hide(); 
    jQuery('.tab_content#' + stringref).fadeIn(); 
    return false; 
}); 

我想要的标签编号5中,如果点击和饼干= 1,以显示tab6。 ,显示DIV5或DIV6如果cookie是1或空的代码是:

alreadyClosed = $.cookie(stateCookieName); 

if (alreadyClosed != 1) { 
    $DIV5.show(); 
    $DIV6.hide(); 
} else { 
    $DIV5.hide(); 
    $DIV6.show(); 
} 

如何把它们加起来?我想他们应该是说一个方法:

If tab1.click and cookie = 1 then show $div1 
else tab1.click and cookie = null then show $div2 

身体看起来像:

<!-- buttons --> 
<ul id="boxes" class="tabs"> 
<li><a href="#Div5">Div 5</a></li> 
<li><a href="#Div6">Div 6</a></li> 
</ul> 

<!-- content --> 
<div class="tab_content" id="Div5">DIV 5</div> 
<div class="tab_content" id="Div6">DIV 6</div> 

我希望你们能帮助我。 非常感谢& Regards, rallyboy。

回答

0

经过这么久,我终于找到了它。我用这个替换了这个点击功能:

var alreadyClosed = $.cookie(stateCookieName) == '1'; 
jQuery('ul.tabs a').click(function() { 
    var ref = jQuery(this).attr('href').split('#')[1]; 
    if (alreadyClosed && ref == 'Div5') { 
     ref = 'Div6'; 
    } 
    jQuery('div.tab_content:not(#' + ref + ')').hide(); 
    jQuery('#' + ref).fadeIn(); 
    return false; 
}); 
1
jQuery(".tabs a").click(function() { 
    alreadyClosed = $.cookie(stateCookieName); 

    stringref = jQuery(this).attr("href").split('#')[1]; 

    if(alreadyClosed == 1) 
    { 
     newstring = stringref; 
     if(stringref == "5") 
     { 
      newstring = "6"; 
     } 
     elseif(stringref == "6") 
     { 
      newstring = "5"; 
     } 
     stringref = newstring; 
    } 

    jQuery('.tab_content:not(#' + stringref + ')').hide(); 
    jQuery('.tab_content#' + stringref).fadeIn(); 
    return false; 
}); 

此额外的代码做快速检查 - 如果cookie被设置,它切换5至6或6至5。然后它前进正常。

对于制表符5和制表符6,您是否需要比此更通用的解决方案?如果是这样,你能否就如何工作制定一些规则?

+0

精彩的回答先生。我现在正在努力解决这个问题3天。我花了这么多小时。无论如何,当cookie设置为1时,它不会显示Tab6。我想要的只是如果cookie = 1,然后单击tab5然后显示tab6,如果cookie = null,并且我单击了选项卡5,它将显示tab5,就像它现在一样。 – jQuerybeast 2011-04-11 22:06:48

+0

目前,如果没有cookie,它可以很好地工作,但是当我设置cookie时,它不会。让我知道你是否想要一个精确的页面快速小提琴。我想问题出在您在代码上编写的5和6号码上。我尝试用#Div5和#Div6或Div5和Div6替换,但它不起作用。 – jQuerybeast 2011-04-11 22:08:40

+0

我要看看这个明天把它摸索出适合你。我相信它非常接近。你的小提琴会有帮助。 – 2011-04-12 04:21:28

0
jQuery(".tabs a").click(function() { 
    stringref = jQuery(this).attr("href").split('#')[1]; 

    // Normal navigation 
    if(stringref != 'div5') { 
     jQuery('.tab_content:not(#' + stringref + ')').hide(); 
     jQuery('.tab_content#' + stringref).fadeIn(); 
    } else { 
     // 5 is special, do further checking 
     var alreadyClosed = $.cookie(stateCookieName); 

     if (alreadyClosed != 1) { 
      $('div5').show(); 
      $('div6').hide(); 
     } else { 
      $('div5').hide(); 
      $('div6').show(); 
     }   
    } 

    return false; 
}); 
+0

感谢您的回复先生。看来,如果我有cookie,它仍然显示选定的选项卡。请提及每个选项卡都有类tab_content。我在这里做了一个快速的小提琴:http://jsfiddle.net/Nsgcn/关于它的外观。再次感谢很多 – jQuerybeast 2011-04-11 22:14:19