daGrevis Posted December 31, 2010 Report Posted December 31, 2010 Pirmais skripts '11! Ideja ir, ka kastītes krāsas mainās ik pēc sekundes sākot no sarkana, tad pārejot uz zaļu, tad zilu. Un atkal uz apli. Tagad skripts padara kastīti zilu. Un nekas nemainās. http://pastie.org/1420403 Quote
0 v3rb0 Posted January 1, 2011 Report Posted January 1, 2011 setTimeout() pirmais arguments ir vai nu norāde uz f-ju, vai eval'ojams strings, -tb novāc () vai ieliec pēdiņās. Quote
0 sheps Posted January 1, 2011 Report Posted January 1, 2011 (edited) <script type="text/javascript"> var color_list = ['red','green','blue']; var color = 0; function setColor() { document.getElementById( 'box' ).style.background = color_list[color]; color += 1; if(color>=color_list.length){color = 0;} setTimeout('setColor()', 1000 ); /* OR setInterval(function(){ document.getElementById( 'box' ).style.background = color_list[color]; color += 1; if(color>=color_list.length){color = 0;} },1000); */ } </script> Edited January 1, 2011 by sheps Quote
0 daGrevis Posted January 1, 2011 Author Report Posted January 1, 2011 Paldies abiem. =) var colors = [ '#ff0000','#ffa500','#ffff00', '#008000', '#0000ff', '#4b0082', '#ee82ee' ]; var color_id = 0; function setColor() { document.getElementById( 'box' ).style.background = colors[ color_id ]; color_id++; if( color_id >= colors.length ) { color_id = 0; } setTimeout( 'setColor()', 10 ); } Kaut kur dzirdēju, ka nav labi izmantot HTML atribūtus 'onload', 'onclick' utml.. Kāda ir alternatīva? Quote
0 marrtins Posted January 1, 2011 Report Posted January 1, 2011 (edited) Nav labi, ja nemāk un/vai negrib čakarēties. Bet, visticamāk, alternatīva bija domāta apmēram šāda: $(document).ready(function(){ $('a').click(function(){ alert('Oh yea'); return false; }); }); (izmantots jQuery - idejai būtu jābūt skaidrai) Edited January 1, 2011 by marrtins Quote
0 daGrevis Posted January 1, 2011 Author Report Posted January 1, 2011 Jā, jQuery Es tā visu laiku daru. Nē, man nav skaidrs kā to dabūt tīrā JS... =( Quote
0 daGrevis Posted January 1, 2011 Author Report Posted January 1, 2011 (edited) http://www.webreference.com/programming/javascript/onloads/ http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html http://stackoverflow.com/questions/191157/window-onload-vs-body-onload Tikai nezinu, kādēļ nestrādā... =( document.onload = function() { var colors = [ '#ff0000','#ffa500','#ffff00', '#008000', '#0000ff', '#4b0082', '#ee82ee' ]; var color_id = 0; function setColor() { document.getElementById( 'box' ).style.background = colors[ color_id ]; color_id++; if( color_id >= colors.length ) { color_id = 0; } setTimeout( 'setColor()', 100 ); } } Edited January 1, 2011 by daGrevis Quote
0 marcis Posted January 2, 2011 Report Posted January 2, 2011 $(function(){ // shortcut for $(document).ready() (function setColor(){ ... })(); // izsaucam definēto funkciju }); Quote
0 indoom Posted January 3, 2011 Report Posted January 3, 2011 ir window.onload = function(); nevis document.onload Un nevis pati setColor funkcija jāliek onloadā, bet tur ir jāpalaiž tā funkcija function setColor() { .. } window.onload = setColor; http://stackoverflow.com/questions/191157/window-onload-vs-body-onload Tikai nezinu, kādēļ nestrādā... =( document.onload = function() { Quote
Question
daGrevis
Pirmais skripts '11! Ideja ir, ka kastītes krāsas mainās ik pēc sekundes sākot no sarkana, tad pārejot uz zaļu, tad zilu. Un atkal uz apli. Tagad skripts padara kastīti zilu. Un nekas nemainās.
http://pastie.org/1420403
9 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.