English 中文(简体)
展示/利用红树节节
原标题:Show/Hide using toggle saving to cookie
  • 时间:2011-08-04 23:45:10
  •  标签:
  • jquery

能否改进,只有单角功能?

var show = $("#shows ul li");   
show.addClass("active");

$(show).each(function(c){
    var cvalue = $.cookie( show  + c);
   if ( cvalue ==  closed  + c ) { 
        $(this).css({display:"none"});
        $(this).removeClass( active ).addClass( inactive );
   };
}); 

$("#shows li.active").toggle(function(){
    var num = show.index(this);
    var cookieName =  show  + num;
    var cookieValue =  closed  + num;
    $(this).slideUp(500);
    $(this).removeClass( active );
    $.cookie(cookieName, cookieValue, { path:  / , expires: 10 }); 
},function(){
    var num = $(this).index(this);
    var cookieName =  show  + num;
    $(this).slideDown(500);
    $(this).addClass("active");        
    $.cookie(cookieName, null, { path:  / , expires: 10 });
});

$("#shows li.inactive").toggle(function(){
    var num = show.index(this);
    var cookieName =  show  + num;
    $(this).slideDown(500);
    $(this).addClass("active");
    $(this).removeClass( inactive );       
    $.cookie(cookieName, null, { path:  / , expires: 10 });
},function(){
    var num = show.index(this);
    var cookieName =  show  + num;
    var cookieValue =  closed  + num;
    $(this).slideUp(500);
    $(this).removeClass( active );
    $.cookie(cookieName, cookieValue, { path:  / , expires: 10 }); 
});
最佳回答

你可以做这样的事情。

$("#shows li").toggle(function(){

    var isactive = $(this).hasClass("active") ? true : false;

    var num = show.index(this);
    var cookieName =  show  + num;
    var cookieValue = null;

    if(isactive){
        cookieValue =  closed  + num;
        $(this).slideUp(500);
        $(this).removeClass( active );
    }else{
        $(this).slideDown(500);
        $(this).addClass("active");
        $(this).removeClass( inactive );       
    }

    $.cookie(cookieName, cookieValue, { path:  / , expires: 10 }); 

},function(){
    var isactive = $(this).hasClass("active") ? true : false;

    var num = $(this).index(this);
    var cookieName =  show  + num;
    var cookieValue = null;

    if(isactive){
        $(this).slideDown(500);
        $(this).addClass("active");        
    }else{
        cookieValue =  closed  + num;
        $(this).slideUp(500);
        $(this).removeClass( active ); 
    }
    $.cookie(cookieName, cookieValue, { path:  / , expires: 10 }); 
});
问题回答

否则,情况会好:

function func1() {
    var num = show.index(this);
    var cookieName =  show  + num;
    var cookieValue =  closed  + num;
    $(this).slideUp(500);
    $(this).removeClass( active );
    $.cookie(cookieName, cookieValue, { path:  / , expires: 10 }); 
}

function func2() {
    var num = $(this).index(this);
    var cookieName =  show  + num;
    $(this).slideDown(500);
    $(this).addClass("active");        
    $.cookie(cookieName, null, { path:  / , expires: 10 });
}

$("#shows li.active").toggle(func1,func2);
$("#shows li.inactive").toggle(func2,func1);

如果不看到你的标记,你就能够肯定地将这些标记减少到一个短的<条码>。 例如,使用<条码>至ggleClass和<条码>滑坡Toggle<>。 方法:

$("#shows li").toggle(function(){
    var num = show.index(this);
    $(this).slideToggle(500);
    $(this).toggleClass( active );
    var cookieName =  show  + num;
    var cookieValue = ($(this).hasClass("active") ?  show  :  closed ) + num;
    $.cookie(cookieName, cookieValue, { path:  / , expires: 10 }); 
}

您是否真正需要<条码>、活性和<条码>。 我建议,这些要素只是为了在<代码>上(活性)/代码”类别之间(而任何内容都意味着不活动)。





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签