How to use a very efficient for loop statement

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.