How do I fade in a div by id if cookie=1?
I have a page with few tabs. I am looking for a way that if you select tab 5 and cookie = 1 then show tab6 otherwise show tab5.
The code for the tabs is:
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;
});
I want the tab number 5, if clicked and cookie=1 to show tab6. The code that shows the div5 or Div6 if cookie is 1 or null is:
alreadyClosed = $.cookie(stateCookieName);
if (alreadyC开发者_运维问答losed != 1) {
$DIV5.show();
$DIV6.hide();
} else {
$DIV5.hide();
$DIV6.show();
}
How do I add them together? I assume their should be a way to say:
If tab1.click and cookie = 1 then show $div1
else tab1.click and cookie = null then show $div2
The body looks like:
<!-- 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>
I hope you guys can help me. Thanks alot & Regards, rallyboy.
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;
});
This extra code does a quick check - if the cookie is set, it toggles 5 to 6 or 6 to 5. Then it proceeds as normal.
Do you need a more general solution than this for tabs 5 and 6? If so, can you please lay out some rules for how exactly it should work?
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;
});
After so long I found it out finally. I replaced this click function with this:
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;
});
精彩评论