Html 如何使用 Javascript 遍历 Elements 子节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16672647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I iterate through an Elements child nodes using Javascript?
提问by Hans En
I'm trying to iterate through a DOM elements children and display their attributes in my code:
我正在尝试遍历 DOM 元素子元素并在我的代码中显示它们的属性:
document.write("<table border='1'>");
var testcaseiterator = xmlDoc.getElementsByTagName("TestCase")[1].childNodes;
for(i=0; i < testcaseiterator.length; i++){
document.write("<tr><td>");
document.write(testcaseiterator[i].getAttribute('name'));
document.write("</td></tr>");
}
document.write("</table>");
My XML:
我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="XSLTOutput2.xsl"?>
<TestSuite>
<ActiveStep case="6" step="14" />
<TestCase name="Visit all buttons [in region Button: Navi 0]">
<Verification type="screen" id="1e542f75-1ba2-482d-8edd-39f70620d6dd" status="">
<screen widgetid="audio_radio_fm">
<menu entryStrategy="FIRST" looped="false" orientation="HORIZONTAL" selectable="true" widgetid="status">
<entry selectable="true" widgetid="status" widgettype="entry">
<label widgetid="status_label">status</label>
</entry>
</menu>
<menu entryStrategy="SECOND" looped="false" orientation="HORIZONTAL" selectable="true" widgetid="apl">
<button selectable="true" widgetid="apl_navi" widgettype="button">
<label widgetid="apl_navi_label">Navi</label>
</button>
<button selectable="true" widgetid="apl_audio" widgettype="button">
<label widgetid="apl_audio_label">Audio</label>
</button>
<button selectable="true" widgetid="apl_tel" widgettype="button">
<label widgetid="apl_tel_label">Tel</label>
</button>
<button selectable="true" widgetid="apl_video" widgettype="button">
<label widgetid="apl_video_label">Video</label>
</button>
<button selectable="true" widgetid="apl_system" widgettype="button">
<label widgetid="apl_system_label">System</label>
</button>
<button selectable="true" widgetid="apl_net" widgettype="button">
<img src="img/apl_net.png" widgetid="apl_net_image" />
</button>
</menu>
<menu entryStrategy="FIRST" looped="false" orientation="HORIZONTAL" selectable="true" widgetid="plf">
<entry selectable="true" widgetid="playfield" widgettype="entry">
<label widgetid="playfield_label">playfield</label>
</entry>
</menu>
<menu entryStrategy="MIDDLE" looped="false" orientation="HORIZONTAL" selectable="true" widgetid="audio_radio_sfl">
<button selectable="true" widgetid="audio_radio_sfl_radio" widgettype="button">
<label widgetid="audio_radio_sfl_radio_label">Radio</label>
</button>
<button selectable="true" widgetid="audio_radio_sfl_presets" widgettype="button">
<label widgetid="audio_radio_sfl_presets_label">Presets</label>
</button>
<button selectable="true" widgetid="audio_radio_sfl_info" widgettype="button">
<label widgetid="audio_radio_sfl_info_label">Info</label>
</button>
<button selectable="true" widgetid="audio_radio_sfl_fm" widgettype="button">
<label widgetid="audio_radio_sfl_fm_label">FM</label>
</button>
<button selectable="true" widgetid="audio_radio_sfl_sound" widgettype="button">
<label widgetid="audio_radio_sfl_sound_label">Sound</label>
</button>
</menu>
</screen>
</Verification>
<Verification optionID="Audio" type="focus" id="d713b58b-06b1-4274-9a79-fe90a071383c" status="" />
<Command type="cce:left" id="3a591a3c-9b9f-41a7-9b50-0892197128ec" status="" />
<Verification optionID="Navi" type="focus" id="6037419a-25ae-4a7f-b842-0e88df9dfe4c" status="" />
</TestCase>
This is not working: Any ideas?
这不起作用:有什么想法吗?
回答by John b
var testcaseiterator = xmlDoc.getElementsById("tc").childNodes;
The rest of the code looks okay.
其余的代码看起来没问题。
for(i=0; i < testcaseiterator.length; i++)
{
document.write("<tr><td>");
document.write(testcaseiterator[i].getAttribute('name'));
document.write("</td></tr>");
}
document.write("</table>")
You will also have to edit the 5th line of the XML,
您还必须编辑 XML 的第 5 行,
<TestCase id ="tc" name="Visit all buttons [in region Button: Navi 0]">
This will only work if you XML in your DOM structure.
这仅在您的 DOM 结构中使用 XML 时才有效。