当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js 学习之路8:for循环

js 学习之路8:for循环

2019年01月30日  | 移动技术网IT编程  | 我要评论

1. for循环

<!doctype html>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<body>

<script charset = "utf-8">

namelist = ["kl", "td", "mgeno", "bobo"];
for (var i = 0; i < namelist.length; i++)
{
    document.write(namelist[i] + "<br>");
}
</script>

</body>
</html>

结果:

 

2. for/in循环直接遍历循环对象的属性。

<!doctype html>
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<body>

<script charset = "utf-8">

var maninfo = {name: "tim", age: 20, grade: 98};
for (var i in maninfo)
{
    var info = i + ": " + maninfo[i];
    document.write(info + "<br>");
}

</script>

</body>
</html>

结果:

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网