Loop statements are often used in program implementation, among which the for loop is found in most languages. In JavaScript, there are several different uses of for loops. Here are my understandings of each.
First type: (usually, loop to perform related operations)
var objA=document.getElementsByTagName("a");
var i,max;
for(i=0,max=objA.length;iobjA[i].onclick=function(){
alert(this.innerHTML);
}
}
loop, registering hyperlink tags in turn Click operation
The second type: (for objects, operating object content)
var person={name:'wmhello',age:'28'};
var tips=''; for(var obj in person){
tips =obj '-->' person[obj] 'n'
}
alert(tips)
The third type: (commonly used for arrays, for arrays Perform specific operations)
var num=[1,3 ,5];
var total=0;
num.forEach(function(e){
total =e;
});
alert(total);
This forEach loop works in firefox and chrome
Statement: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