A highly efficient for loop statement usage _javascript tips
Source: Internet
Author: User
Add a style to each row of the table, note the second argument for, and when the array subscript crosses over, Row=row[i] returns false to the end of the loop.
var rows = document.getElementsByTagName (' tr ');
for (var i = 0, row; row = rows[i]; i++) {
Row.classname = ' Newclass ';
}
Test code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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 A () {
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-consuming" + (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-consuming" + (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>
<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>
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.