我正在尝试遍历 XML 并获取 set
节点的 label
属性并创建一个列表,但我无法正确遍历。有人可以解释一下如何遍历和创建列表吗?
这里是 Fiddle
XML 是
<chart caption='View by Law Category' subcaption='Law Category' xAxisName='null' yAxisName='Count' bgColor='AAFFAA,FFFFFF' exportEnabled='1' exportAtClient='1' exportHandler='fcExporter1' showLabels='0' showValues='1' showLegend='1' legendBorderAlpha='0' legendBgAlpha='0' legendShadow='0' formatNumberScale='0'>
<set label='1495cat' value='2' link='j-loadImpactChart-Law Category:1495cat' />
<set label='Category Test' value='35' link='j-loadImpactChart-Law Category:Category Test' />
<set label='Labour' value='149' link='j-loadImpactChart-Law Category:Labour' />
<set label='New Law 1' value='58' link='j-loadImpactChart-Law Category:New Law 1' />
<set label='testing' value='4' link='j-loadImpactChart-Law Category:testing' />
</chart>
我尝试过 alert($('chart>set').attr('label'));
$($xml).each(function(){
alert($(this).find("chart>set").attr('label'));
});
最佳答案
这是您要找的吗?
var xml = "<chart caption='View by Law Category' subcaption='Law Category' xAxisName='null' yAxisName='Count' bgColor='AAFFAA,FFFFFF' exportEnabled='1' exportAtClient='1' exportHandler='fcExporter1' showLabels='0' showValues='1' showLegend='1' legendBorderAlpha='0' legendBgAlpha='0' legendShadow='0' formatNumberScale='0'><set label='1495cat' value='2' link='j-loadImpactChart-Law Category:1495cat' /><set label='Category Test' value='35' link='j-loadImpactChart-Law Category:Category Test' /><set label='Labour' value='149' link='j-loadImpactChart-Law Category:Labour' /><set label='New Law 1' value='58' link='j-loadImpactChart-Law Category:New Law 1' /><set label='testing' value='4' link='j-loadImpactChart-Law Category:testing' /></chart>";
var xmlDoc = $.parseXML(xml)
$(xmlDoc).find("chart > set").each(function(){
alert($(this).attr('label'));
});
关于jQuery - 遍历 xml 并创建它的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35504067/