日期:2014-05-16  浏览次数:20429 次

for in 用法
for in用法
例如:我要查document对像下的属性可以用如下语句
var prop;
for(prop in document)
{
  document.write(prop+":"+document[prop]+"<br />");
}

现我想得到document下面的属性的属性,怎么办
var prop;
for(prop in document.form)
{
  document.write(prop+":"+document.form[prop]+"<br />");
}
这个是错的,请朋友们帮我给出正确的答案吧,

------解决方案--------------------
HTML code

<script type="text/javascript">
var prop; 
for(prop in document.forms[0]) 
{ 
   document.write(prop+":"+document.forms[0][prop]+" <br/>"); 
} 
</script>

------解决方案--------------------
1楼滴没问题,L@_@K
HTML code
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>js.showAllPropertiesOfForm.html </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="Gao YiXiang" />
  <meta name="email" content="yixianggao@126.com" />
  <meta name="keywords" content="javascript dhtml dom" />
  <meta name="description" content="I love web development." />
</head>
<body>
  <form> </form>
  <script type="text/javascript">
  <!--
for(var prop in document.forms[0])
{
document.write(prop + " : " + document.forms[0][prop] + " <br />");
}
  //-->
  </script>
</body>
</html>