讲解JavaScript中for...in语句的使用方法

时间:2015-06-03    点击:54   

 这里是JavaScript支持的另外一个循环。它被称为for...in循环。这个循环是用于循环一个对象的属性。

因为我们还没有讨论的对象,所以使用这一循环可能会感觉不太明白。但是,一旦你会对JavaScript对象了解后,那么会发现这个循环非常有用。
语法

for (variablename in object){
 statement or block to execute
}

从对象每次迭代一个属性分配给变量名(variablename),这个循环持续到该对象的所有属性都用尽。
例子:

下面是打印出Web浏览器的导航器-Navigator 对象的属性,如下面的例子:

<script type="text/javascript">
<!--
var aProperty;
document.write("Navigator Object Properties<br /> ");
for (aProperty in navigator)
{
 document.write(aProperty);
 document.write("<br />");
}
document.write("Exiting from the loop!");
//-->
</script>

这将产生以下结果:

Navigator Object Properties
appCodeName
appName
appMinorVersion
cpuClass
platform
plugins
opsProfile
userProfile
systemLanguage
userLanguage
appVersion
userAgent
onLine
cookieEnabled
mimeTypes
Exiting from the loop! 

JavaScript函数使用的基本教程
js动态创建及移除div的方法
JS实现窗口加载时模拟鼠标移动的方法
利用js实现禁止复制文本信息
详解JavaScript中循环控制语句的用法
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved