Thursday, 15 April 2010

javascript - Loop through background colors (for loop) -


I am trying to learn javascript and dome. I want to use a loop to create an alternative background color for my text. I

HTML:

  & lt; P & gt; A & lt; / P & gt; & Lt; P & gt; Two & lt; / P & gt; & Lt; P & gt; Three & lt; / P & gt; & Lt; P & gt; Four & lt; / P & gt; & Lt; P & gt; Five & lt; / P & gt;   

Javascript:

  var p = document.getElementByTagName (p); Var color = ["red", "green"]; (Var i = 0; i & lt; p.length; i ++) {p.style.background = colors; }   

Below is:

Any help will be great :)

Try something like:

 for  (var i = 0; i & lt; p.length; i ++) {p} [ I] .style.background = Color [% 2 I]; }   

i% 2 is left or is divided by i (loop variable) 2. What are you doing for you



No comments:

Post a Comment