我的html代码是这样的
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","brands.xml",false);
xmlhttp.send();
theXmlDoc=xmlhttp.responseXML;
function fillForm(){
$(theXmlDoc).find('table[name=brands]').each(function(){
alert($(this));//doesn't fire when brands.xml contains more than one entry of <table name="brands"> else shows Object object
});
我的 brands.xml 是
<table name="brands">
<column name="BrandID">1</column>
<column name="BrandName">AX</column>
<column name="CompanyInfo">FDC</column>
<column name="Composition">Cap</column>
</table>
<table name="brands">
<column name="BrandID">2</column>
<column name="BrandName">UP</column>
<column name="CompanyInfo">Tor</column>
<column name="Composition">Asp</column>
</table>
当brands.xml
包含 <table name="brands">
的单个条目警报显示 Object 对象,但是当我包含多个表名时,如上所示,每个表名都不会被执行。
最佳答案
您的 XML 需要由单个节点包装:
<tables>
<table name="brands">
<column name="BrandID">1</column>
<column name="BrandName">AX</column>
<column name="CompanyInfo">FDC</column>
<column name="Composition">Cap</column>
</table>
<table name="brands">
<column name="BrandID">2</column>
<column name="BrandName">UP</column>
<column name="CompanyInfo">Tor</column>
<column name="Composition">Asp</column>
</table>
</tables>
并且您需要相应地调整您的 JavaScript,因此请在此包装节点内进行选择。
关于javascript - 从 jquery each 遍历 xml 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10123468/