我有一个大型的xml文件,内容如下:
<satellites>
<sat name="NSS 9/Yamal 300K (177.0W)" flags="0" position="-1770">
<transponder frequency="3792000" symbol_rate="2048000" polarization="1" fec_inner="2" system="1" modulation="1"/>
<transponder frequency="3987000" symbol_rate="8950000" polarization="1" fec_inner="2" system="0" modulation="1"/>
<transponder frequency="3999000" symbol_rate="2960000" polarization="1" fec_inner="3" system="0" modulation="1"/>
<transponder frequency="4055000" symbol_rate="11000000" polarization="0" fec_inner="1" system="1" modulation="1"/>
<transponder frequency="4099000" symbol_rate="30000000" polarization="1" fec_inner="7" system="1" modulation="1"/>
<transponder frequency="4163000" symbol_rate="2644000" polarization="1" fec_inner="3" system="1" modulation="1"/>
</sat>
<sat name="AMC 8/18 (139.0W)" flags="0" position="-1390">
<transponder frequency="4056000" symbol_rate="13250000" polarization="0" fec_inner="3" system="0" modulation="1"/>
</sat>
<sat name="AMC 10 (135.0W)" flags="0" position="-1350">
<transponder frequency="3780000" symbol_rate="30000000" polarization="0" fec_inner="3" system="1" modulation="2"/>
<transponder frequency="3820000" symbol_rate="29270000" polarization="0" fec_inner="3" system="0" modulation="1"/>
<transponder frequency="4080000" symbol_rate="29270000" polarization="1" fec_inner="3" system="0" modulation="1"/>
<transponder frequency="4136000" symbol_rate="19510000" polarization="0" fec_inner="3" system="0" modulation="1"/>
<transponder frequency="4149000" symbol_rate="9760000" polarization="0" fec_inner="3" system="0" modulation="1"/>
<transponder frequency="4180000" symbol_rate="29270000" polarization="0" fec_inner="5" system="0" modulation="1"/>
</sat>
</satellites>
我让用户选择卫星,然后我必须获得所选频率的可用频率。我做的第一部分,但现在我无法通过它的sat
属性获取name
元素来显示转发器频率列表和其他信息。
这是我的代码:var satellitesData = fileData.find('sat[name='+satName+']');
还尝试了这个:var satellitesData = $(fileData).find('sat[name='+satName+']');
这:var satellitesData = fileData.find('sat[name='+satName+'] > transponder');
而且:var satellitesData = fileData.find('sat[name='+satName+']').innerHTML;
没有任何作用。我做错了什么?
答案 0 :(得分:3)
由于您要查找的name
属性通常包含空格,因此您需要使用引号将搜索的属性括起来,否则语法将无效。使用querySelector
而不是.find
使用标准Javascript(.find
用于jQuery,或者用于纯Javascript数组,但不能用于Javascript HTMLElements)。
const satName = "NSS 9/Yamal 300K (177.0W)";
const satellites = document.querySelector('satellites');
const satellitesData = satellites.querySelector('sat[name="' + satName + '"]');
console.log(satellitesData);
&#13;
<satellites>
<sat name="NSS 9/Yamal 300K (177.0W)" flags="0" position="-1770">
<transponder frequency="3792000" symbol_rate="2048000" polarization="1" fec_inner="2" system="1" modulation="1" />
<transponder frequency="3987000" symbol_rate="8950000" polarization="1" fec_inner="2" system="0" modulation="1" />
<transponder frequency="3999000" symbol_rate="2960000" polarization="1" fec_inner="3" system="0" modulation="1" />
<transponder frequency="4055000" symbol_rate="11000000" polarization="0" fec_inner="1" system="1" modulation="1" />
<transponder frequency="4099000" symbol_rate="30000000" polarization="1" fec_inner="7" system="1" modulation="1" />
<transponder frequency="4163000" symbol_rate="2644000" polarization="1" fec_inner="3" system="1" modulation="1" />
</sat>
<sat name="AMC 8/18 (139.0W)" flags="0" position="-1390">
<transponder frequency="4056000" symbol_rate="13250000" polarization="0" fec_inner="3" system="0" modulation="1" />
</sat>
<sat name="AMC 10 (135.0W)" flags="0" position="-1350">
<transponder frequency="3780000" symbol_rate="30000000" polarization="0" fec_inner="3" system="1" modulation="2" />
<transponder frequency="3820000" symbol_rate="29270000" polarization="0" fec_inner="3" system="0" modulation="1" />
<transponder frequency="4080000" symbol_rate="29270000" polarization="1" fec_inner="3" system="0" modulation="1" />
<transponder frequency="4136000" symbol_rate="19510000" polarization="0" fec_inner="3" system="0" modulation="1" />
<transponder frequency="4149000" symbol_rate="9760000" polarization="0" fec_inner="3" system="0" modulation="1" />
<transponder frequency="4180000" symbol_rate="29270000" polarization="0" fec_inner="5" system="0" modulation="1" />
</sat>
</satellites>
&#13;
您可以考虑模板文字:
const satellitesData = satellites.querySelector(`sat[name="${satName}"]`);