我正在试验这个 xml:
<theFeed>
<games>
<game id="103" period="" clock="">
<team id="657" type="home" logo="1/12" score="46"/>
<team id="740" type="visitor" seed="11" score="59"/>
</game>
</games>
</theFeed>
Run Code Online (Sandbox Code Playgroud)
我试图从游戏节点的第一个子节点获取属性“分数”,但是当我使用此代码(javascript)时:
var Hlogo = theXml.getElementsByTagName('game')[0].childNodes[0].getAttribute('score');
Run Code Online (Sandbox Code Playgroud)
它崩溃了。我可以使用getAttributes...从父级那里获得属性就好了……我做错了什么吗?
var game = theXml.getElementsByTagName('game')[0];
var team = game.getElementsByTagName('team')[0];
var score = team.getAttribute('score');
console.log(game, team, score);
Run Code Online (Sandbox Code Playgroud)
似乎工作正常,提供theXml有效(我强迫它是document)
希望这有帮助 -ck