如何遍历下的所有子节点,并读取其中的信息
我在做一个原型实验时,遇到了一个问题,那就是在遍历一个<g></g>下的所有子节点时,总是出错,请高手支着。
原码如下:
<g id="g1" num="2" width="1200" height="800" onmousemove="move(evt)" onmouseout="changeTextNotOver(evt);stop(evt)" onclick="recordClick(evt)" onmousedown="down(evt)" onmouseup="stop(evt)">
<rect id="g1r1" x="50" y="100" width="200" height="80" style="fill:red" />
<text id="g1t1" x="50" y="140" >wind</text>
<rect id="g1r2" x="50" y="180" width="200" height="80" style="fill:red" />
<text id="g1t2" x="50" y="220" >cloud</text>
脚本如下:
function move(evt)
{
target=evt.getTarget();
p=target.getParentNode();
pid=p.getAttribute("id");
tx=evt.getClientX();
ty=evt.getClientY();
num=parseFloat(p.getChildNodes().Length);
for (var i=0;i<num;i++)
{//遍历一个组中所有的元素
alert(p.childNodes[i].getAttribute("id"));
}//遍历一个组中所有的元素
错误提示为:
alert(p.childNodes[i].getAttribute("id"));
该行中,不支持该方法或属性。请高手指教。