如何使用 Javascript/jQuery 获取 XML 节点的属性值?
我正在尝试获取节点上的持续时间属性的值,然后获取固定值。
<loanAmount>
<interestRates>
<interestRate allowInterestOnly="true" type="variable" value="5.82"/>
<interestRate allowFixed="true" allowInterestOnly="true" duration="1" fixedInterestOnlyValue="5.7" fixedValue="5.7" variableInterestOnlyValue="5.82"/>
<interestRate allowFixed="true" allowInterestOnly="true" duration="3" fixedInterestOnlyValue="5.75" fixedValue="5.75" variableInterestOnlyValue="5.82"/>
<interestRate allowFixed="true" allowInterestOnly="true" duration="5" fixedInterestOnlyValue="6.64" fixedValue="6.56" variableInterestOnlyValue="5.82"/>
<interestRate allowFixed="true" allowInterestOnly="true" duration="10" variableInterestOnlyValue="5.82"/>
</interestRates>
</loanAmount>'
到目前为止,我有:
var currentLoanRates = function() {
var currLoanXml = '<loanAmount><interestRates><interestRate allowInterestOnly="true" type="variable" value="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="1" fixedInterestOnlyValue="5.7" fixedValue="5.7" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="3" fixedInterestOnlyValue="5.75" fixedValue="5.75" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="5" fixedInterestOnlyValue="6.64" fixedValue="6.56" variableInterestOnlyValue="5.82"/><interestRate allowFixed="true" allowInterestOnly="true" duration="10" variableInterestOnlyValue="5.82"/></interestRates></loanAmount>',
xmlDoc = $.parseXML( currLoanXml ),
$xml = $( xmlDoc ),
$intRate = $xml.find("interestRate"),
$varIntRate = $intRate.attr("fixedValue");
console.log($intRate);
console.log($varIntRate);
};
第二个 console.log 打印undefined。