Add a style to each row of the table. Pay attention to the second parameter of for. When the array subscript is out of bounds, row = row [I] returns false, and the loop ends.
VaR rows = Document. getelementsbytagname ('tr ');
For (VAR I = 0, row; ROW = rows [I]; I ++ ){
Row. classname = 'newclass ';
}
TestCode:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
<Style>
Li {margin-left: 28px ;}
. ABC {background-color: # Eee; font-size: 14px; margin-bottom: 10px ;}
</Style>
<Script language = "JavaScript">
<! --
Function (){
VaR ROW = Document. getelementsbytagname ("Li ");
VaR T1 = new date ();
For (VAR I = 0, J; j = row [I]; I ++)
{
J. classname = "ABC ";
}
VaR t2 = new date ();
Alert ("Time consumed" + (t2-t1). tostring () + "millisecond ");
}
Function B (){
VaR ROW = Document. getelementsbytagname ("Li ");
VaR T1 = new date ();
For (VAR I = 0; I <row. length; I ++)
{
Row [I]. classname = "ABC ";
}
VaR t2 = new date ();
Alert ("Time consumed" + (t2-t1). tostring () + "millisecond ");
}
Function C (){
VaR J = "<ol> ";
For (VAR I = 0; I <2000; I ++)
{
J = J + "<li> test text </LI> ";
}
J = J + "</OL> ";
Document. getelementbyid ("box"). innerhtml = J;
}
// -->
</SCRIPT>
</Head>
<Body>
<Input type = "button" value = "createtable" onclick = "C ()"/>
<Input type = "button" value = "test old method! "Onclick =" B () "/>
<Input type = "button" value = "test new method! "Onclick =" A () "/>
<Input type = "button" value = "clear" onclick = "document. getelementbyid ('box'). innerhtml =''; "/>
<Div id = "box"> </div>
</Body>
</Html>