Secondly:"childNodes" property is not a collection, so do not use "in".<html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head><body bgcolor="#FFFFFF" text="#000000" onload="accessAllNodes(document)"> <table> <tr> <td>a</td> </tr> </table></body> </html><SCRIPT LANGUAGE="JScript"> function accessAllNodes(para) { if(para.hasChildNodes()) { for(var i=0;i<para.childNodes.length;i++) accessAllNodes(para.childNodes[i]); } else alert(para.nodeValue);
看看下面代码var tmp = new Array(); tmp["a"] = 1; tmp["b"] = 2;for (var i in tmp) alert(tmp[i]);只有tmp[i]才可以访问到数组中的内容,也就是说i是下标,i="a"...。而C#中int i in tmp的i确是tmp中的某一个内容,也就是说i=1,2....。
会报错啊能不能帮我修改一下呢?谢谢!
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body bgcolor="#FFFFFF" text="#000000" onload="accessAllNodes(document)">
<table>
<tr>
<td>a</td>
</tr>
</table></body>
</html><SCRIPT LANGUAGE="JScript">
function accessAllNodes(para)
{
if(para.hasChildNodes())
{
for(var i=0;i<para.childNodes.length;i++)
accessAllNodes(para.childNodes[i]);
}
else alert(para.nodeValue);
}
</SCRIPT>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body bgcolor="#FFFFFF" text="#000000">
<SCRIPT LANGUAGE="JScript">
function accessAllNodes(para) {
if(para.hasChildNodes)
{
for(i in para.childNodes)
accessAllNodes(para.childNodes[i]);
}
else alert(para.nodeName);
}
</SCRIPT>
<a href="#" onclick="accessAllNodes(document)">gfdsgfdgfd</a>
</body>
</html>
但是我还有一点要问:
对于数组不是可以用in来访问吗?
到底什么才算collection
tmp["a"] = 1;
tmp["b"] = 2;for (var i in tmp)
alert(tmp[i]);只有tmp[i]才可以访问到数组中的内容,也就是说i是下标,i="a"...。而C#中int i in tmp的i确是tmp中的某一个内容,也就是说i=1,2....。