for循环
for(var i=0;i<len;i++)
{
......
}
练习
1、将数组中的数据分别用弹框弹出
<script>
var aList = [1,2,3,4,5];
for(i=0; i<aList.length; i++){
alert(aList[i])
}
</script>
2、将数组中的数据放入到页面中的列表中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.onload = function () {
var aList = ["苹果", "三星", "小米", "华为", "魅族"];
var oUl = document.getElementById("list");
var oLen = aList.length;
var sTr = "";
for(var i=0; i<oLen; i++){
sTr += "<li>" + aList[i] + "</li>" ;
}
oUl.innerHTML = sTr;
}
</script>
<style>
#list li{
width: 300px;
height: 60px;
line-height: 60px;
border-bottom: solid 1px black;
list-style: none;
}
</style>
</head>
<body>
<ul id="list">
<!-- <li>美人鱼</li>-->
<!-- <li>美人鱼</li>-->
<!-- <li>美人鱼</li>-->
<!-- <li>美人鱼</li>-->
<!-- <li>美人鱼</li>-->
</ul>
</body>
</html>
while循环
var i=0;
while(i<8){
......
i++;
}
数组去重
var aList = [1,2,3,4,4,3,2,1,2,3,4,5,6,5,5,3,3,4,2,1];
var aList2 = [];
for(var i=0;i<aList.length;i++)
{
if(aList.indexOf(aList[i])==i)
{
aList2.push(aList[i]);
}
}
alert(aList2);