sandrulis Posted October 9, 2011 Report Share Posted October 9, 2011 kā lai izveidot show / hide tā, lai cookie atceras vai ir hide vai ari show es atradu skriptu kur ir vienam, bet man vajag multiple lūk ir uz vienu: var state; window.onload=function() { obj=document.getElementById('info'); state=(state==null)?'hide':state; obj.className=state; document.getElementById('setup').onclick=function() { obj.className=(obj.className=='show')?'hide':'show'; state=obj.className; setCookie(); return false; } } function setCookie() { exp=new Date(); plusMonth=exp.getTime()+(31*24*60*60*1000); exp.setTime(plusMonth); document.cookie='State='+state+';expires='+exp.toGMTString(); } function readCookie() { if(document.cookie) { state=document.cookie.split('State=')[1]; } } readCookie(); Lūk šis ir mans multiple, bet nezinu kur tas cookie ir jaebāž, lai atceras: function toggle(id){ if (document.getElementById){ var el = document.getElementById(id); $(el).slideToggle("slow"); } } Protams manam multiple nav obligāti jābut ar slideToggle var but kvai ar parasto show(); un hide(); Paldies jau iepriekš par palīdzību :) Link to comment Share on other sites More sharing options...
Grey_Wolf Posted October 10, 2011 Report Share Posted October 10, 2011 principā tev vajag 3 lietas 1. ierakstīt kukija masivu (objektu) kura saglaba tos stavokljus ( ieslegts / izslegts) ta ka JS nav asociativa masiva kaa tada, bet ta vieta ir OBJEKTS.. ( faktiski tas ir tas pats kas PHP asociativais masivs) tad vari kā 'key' izmantot slepjamaa/paradamaa elementa ID un ka vertibu ieslegts/izslegts ( 0/1) etc.. 2. ielasi kukiju, vai ja nav panjem defoltas vertibas 3. ar ciklu izej cauri masivam/objektam un attiecigajiem elementie ieseto vajadzigo stavolki ( show/ haide vai block / none) ( protams 3 punktu izpildot pie OnLoad) vienigi atceries ka elements tiks noslepti / paradiiti PEC lapas pilnigas ielades.. taa ka sakuma labak visus noslep, un tikai vajadzigos atradi, nevis radi visus un nevajadzigos paslep Link to comment Share on other sites More sharing options...
codez Posted October 10, 2011 Report Share Posted October 10, 2011 (edited) Brīnos, ka mūsdienās vēl ir cilvēki, kuri taisa js, neizmantojot kādu js FW. Izmantojot jquery un šo cookie pluginu (https://raw.github.c...query.cookie.js) sanāca šādi: http://jsfiddle.net/4rb22/ js: $('.sh').each(function(){ if ($.cookie($(this).attr('id'))) { $(this).find('.content').show(); } }); $('body').delegate('.sh .header','click',function(){ var $p=$(this).parent(), id=$p.attr('id'), $c=$p.find('.content'); if ($c.is(':visible')){ $.cookie(id,null); $c.hide(); } else { $.cookie(id,1); $c.show(); } }); html: <div id="#a1" class="sh"> <div class="header">Hello</div> <div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only</div> </div> <div id="#a2" class="sh"> <div class="header">Hello</div> <div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only</div> </div> <div id="#a3" class="sh"> <div class="header">Hello</div> <div class="content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only</div> </div> css: .sh{ border:1px solid #888; padding:3px; margin:2px; } .sh .header{ background:#ccc; } .sh .content{ display:none; } Edited October 10, 2011 by codez Link to comment Share on other sites More sharing options...
daGrevis Posted October 10, 2011 Report Share Posted October 10, 2011 Nu ja sāk tikai mācīties, izmantot native valodu, nevis 'freimvorku', ir tikai laba lieta. :) Link to comment Share on other sites More sharing options...
sandrulis Posted October 10, 2011 Author Report Share Posted October 10, 2011 Nevar kaut kā šo kodu saīsināt if(get_cookie("teksts1")) { var tqx = get_cookie ( "teksts1" ); if(tqx == '1') {$(document).ready(function(){$("#teksts1").show();});} else {$(document).ready(function(){$("#teksts1").hide();});} } if(get_cookie("teksts2")) { var tqx = get_cookie ( "teksts2" ); if(tqx == '1') {$(document).ready(function(){$("#teksts2").show();});} else {$(document).ready(function(){$("#teksts2").hide();});} } Link to comment Share on other sites More sharing options...
v3rb0 Posted October 10, 2011 Report Share Posted October 10, 2011 (edited) $(function() { var z = ["teksts1","teksts2"], id; while (id = z.pop()) $("#"+id)[["hide","show"][get_cookie(id)==='1'?1:0]](); }); Edited October 10, 2011 by v3rb0 Link to comment Share on other sites More sharing options...
sandrulis Posted October 11, 2011 Author Report Share Posted October 11, 2011 Kas man te nav pareizs? var category=["f_cat_1", "f_cat_2"]; for (var i=0; i <= 1; i++) { if(get_cookie(category[i])) { var tqx = get_cookie ( category[i] ); if(tqx == '1') { $(document).ready(function(){$("#"+category[i]).show();}); $(document).ready(function(){$("#"+category[i]+"_h").css('display', 'block');}); $(document).ready(function(){$("#"+category[i]+"_s").css('display', 'none');}); $(document).ready(function(){$("#"+category[i]+"_g").css('opacity', '1.0');}); } else { $(document).ready(function(){$("#"+category[i]).hide();}); $(document).ready(function(){$("#"+category[i]+"_h").css('display', 'none');}); $(document).ready(function(){$("#"+category[i]+"_s").css('display', 'block');}); $(document).ready(function(){$("#"+category[i]+"_g").css('opacity', '0.5');}); } } } Ko kad uzrakstu šādu kodu viss ir OK! if(get_cookie("f_cat_1")) { var tqx = get_cookie ( "f_cat_1" ); if(tqx == '1') { $(document).ready(function(){$("#f_cat_1").show();}); $(document).ready(function(){$("#f_cat_1_h").css('display', 'block');}); $(document).ready(function(){$("#f_cat_1_s").css('display', 'none');}); $(document).ready(function(){$("#f_cat_1_g").css('opacity', '1.0');}); } else { $(document).ready(function(){$("#f_cat_1").hide();}); $(document).ready(function(){$("#f_cat_1_h").css('display', 'none');}); $(document).ready(function(){$("#f_cat_1_s").css('display', 'block');}); $(document).ready(function(){$("#f_cat_1_g").css('opacity', '0.5');}); } } if(get_cookie("f_cat_2")) { var tqx = get_cookie ( "f_cat_2" ); if(tqx == '1') { $(document).ready(function(){$("#f_cat_2").show();}); $(document).ready(function(){$("#f_cat_2_h").css('display', 'block');}); $(document).ready(function(){$("#f_cat_2_s").css('display', 'none');}); $(document).ready(function(){$("#f_cat_2_g").css('opacity', '1.0');}); } else { $(document).ready(function(){$("#f_cat_2").hide();}); $(document).ready(function(){$("#f_cat_2_h").css('display', 'none');}); $(document).ready(function(){$("#f_cat_2_s").css('display', 'block');}); $(document).ready(function(){$("#f_cat_2_g").css('opacity', '0.5');}); } } Link to comment Share on other sites More sharing options...
briedis Posted October 11, 2011 Report Share Posted October 11, 2011 Klau, tev būtu jānoskaidro, ko vispār dara šī funkcija: $(document).ready(function(){} Link to comment Share on other sites More sharing options...
sandrulis Posted October 11, 2011 Author Report Share Posted October 11, 2011 danke, viss ok :) Link to comment Share on other sites More sharing options...
daGrevis Posted October 11, 2011 Report Share Posted October 11, 2011 $(function() { // All code that should execute after DOM is loaded. }); Link to comment Share on other sites More sharing options...
Recommended Posts