Home > Web Front-end > JS Tutorial > How to use a very efficient for loop statement_javascript skills

How to use a very efficient for loop statement_javascript skills

WBOY
Release: 2016-05-16 19:10:28
Original
1397 people have browsed it

Add styles 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' ;
}
Test code:

nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



Untitled Document
li { margin-left:28px;}
.abc {background-color:#eee;font-size:14px; margin-bottom:10px;}









<script> <BR><!-- <BR>function a(){ <BR> var row = document.getElementsByTagName("li"); <BR> var t1 = new Date(); <BR> for (var i=0,j; j=row[i]; i++) <BR> { <BR> j.className="abc"; <BR> } <BR> var t2 = new Date(); <BR> alert("耗时 "+(t2-t1).toString()+" 毫秒"); <BR>} <BR>function b(){ <BR> var row = document.getElementsByTagName("li"); <BR> var t1 = new Date(); <BR> for (var i=0; i<row.length; i++) <BR> { <BR> row[i].className="abc"; <BR> } <BR> var t2 = new Date(); <BR> alert("耗时 "+(t2-t1).toString()+" 毫秒"); <BR>} <BR>function c(){ <BR> var j="<ol>"; <BR> for (var i=0; i<2000; i++) <BR> { <BR> j=j+"<li>测试文字"; <BR> } <BR> j=j+""; <BR> document.getElementById("box").innerHTML = j; <BR>} <BR>//--> <BR></script>
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template