noname Posted May 16, 2006 Share Posted May 16, 2006 hi, I've just begun to learn java script , i have a exercises, its description : "You should keep changing a text's color". I have a array to containt colors, but when i use setTimeout() and document.write() to display the text, have a error: Object expected. Can you help me to slove this proplem ? thanks Link to comment Share on other sites More sharing options...
scott100 Posted May 16, 2006 Share Posted May 16, 2006 Please post your code and we'll take a look.thanks Link to comment Share on other sites More sharing options...
Jack McKalling Posted May 16, 2006 Share Posted May 16, 2006 (we = Scott, Dan and other members of this forum) Link to comment Share on other sites More sharing options...
noname Posted May 16, 2006 Author Share Posted May 16, 2006 <HTML><HEAD><TITLE> Text </TITLE><script type="text/javascript">var colors=new Array();colors[0]="blue";colors[1]="green";colors[2]="red";colors[3]="grey";colors[4]="orange";colors[5]="aqua";colors[6]="brown";colors[7]="black";colors[8]="purple";colors[9]="chocolate";colors[10]="magenta";colors[11]="violet";colors[12]="indigo";colors[13]="sienna";colors[14]="yellow";var i=0;var t;function writemess(){ if(i==colors.length) return; document.write(str.fontcolor(colors[i++])); t=setTimeout('writemess()',200); //writemess(); }</script></HEAD><BODY onload="writemess()"> </BODY></HTML> Link to comment Share on other sites More sharing options...
noname Posted May 16, 2006 Author Share Posted May 16, 2006 I want to keep changing a text's color, but it display many text with another colors Link to comment Share on other sites More sharing options...
Webworldx Posted May 16, 2006 Share Posted May 16, 2006 It's this line:document.write(str.fontcolor(colors[i++]));There's no such thing as str.fontcolor in your code Link to comment Share on other sites More sharing options...
scott100 Posted May 16, 2006 Share Posted May 16, 2006 (edited) Patched up <HTML><HEAD><TITLE> Text </TITLE><script type="text/javascript">var colors=new Array();colors[0]="blue";colors[1]="green";colors[2]="red";colors[3]="grey";colors[4]="orange";colors[5]="aqua";colors[6]="brown";colors[7]="black";colors[8]="purple";colors[9]="chocolate";colors[10]="magenta";colors[11]="violet";colors[12]="indigo";colors[13]="sienna";colors[14]="yellow";var i=0;function writemess(){ if (i==15)i=0; document.getElementById('name').innerHTML=colors[i]; i++; setTimeout('writemess()',200)}</script></HEAD><BODY onload="writemess()"><div id="name"></div></BODY></HTML> Yeah like blue says that didn't exist plus i wouldn't use document.write() Edited May 16, 2006 by scott100 Link to comment Share on other sites More sharing options...
scott100 Posted May 16, 2006 Share Posted May 16, 2006 I want to keep changing a text's color, but it display many text with another colors<{POST_SNAPBACK}> You can do that too, just changedocument.getElementById('name').style.color=colors;and put some text between the div<HTML><HEAD><TITLE> Text </TITLE><script type="text/javascript">var colors=new Array();colors[0]="blue";colors[1]="green";colors[2]="red";colors[3]="gray";colors[4]="orange";colors[5]="aqua";colors[6]="brown";colors[7]="black";colors[8]="purple";colors[9]="chocolate";colors[10]="magenta";colors[11]="violet";colors[12]="indigo";colors[13]="sienna";colors[14]="yellow";var i=0;function writemess(){ if (i==15)i=0; document.getElementById('name').style.color=colors[i]; i++; setTimeout('writemess()',200)}</script></HEAD><BODY onload="writemess()"><div id="name">Text</div></BODY></HTML> ps it's spelt gray Link to comment Share on other sites More sharing options...
noname Posted May 16, 2006 Author Share Posted May 16, 2006 thanks so much Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now