以下是我从API获得的一些用于棒球得分的XML。
我在按数字抓取单个局点然后获得比分(行程)时遇到问题。
<scoring>
<inning number="1" sequence="1" runs="2"/>
<inning number="2" sequence="2" runs="1"/>
</scoring>
我试图做一个if语句来检查局数是1还是2,等等。。。
updates['/game/' + uid + '/homeTeamInningScore/1st'] = scoring.inning ?????;
最佳答案
嗨,您可以尝试Jquery $ .grep和$ .attr:
function getInningRuns( xmlString, inningNumber) {
// parse xml string into xml nodes of type inning
// if xml is not received as a string, you can omit the $.parseXML()
var innings = $($.parseXML( xmlString )).find("inning");
// get requested inning
var inning = $.grep(innings, function(inning){
return ($(inning).attr("number") == inningNumber);
})[0];
// return runs attribute
return $(inning).attr("runs");
}
关于javascript - 如何定位嵌套的XML数据以更新Firebase,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44556497/