我正在使用jquery浏览XML文件并找到一些元素并在屏幕上显示
$(xml).find('module').each(function(index){
我想在某个位置选择一个元素。我尝试过几个选项,比如
$(xml).find('modulo').[1]{
$(xml).find('modulo').(1){
$(xml).find('modulo').each(function(index).get([1]){
$(xml).find('modulo').get([1])
但到目前为止还没有。有谁知道什么是正确的sintax?
编辑:--------
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "modulos.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('modulo').each(function(index){
var MAC = $(this).find('MAC').text();
var Type = $(this).find('Type').text();
var Date = $(this).find('Date').text();
var Firmware = $(this).find('Firmware').text();
var room = $(this).find('room').text();
var equipment = $(this).find('equipment').text();
var calibration = $(this).find('calibration').text();
var next_calibration = $(this).find('next_calibration').text();
var rf = $(this).find('rf').text();
var battery = $(this).find('battery').text();
$('<div class="items" id="link_'+index+'"></div>').html(index+" - "+"MAC: "+MAC+
" Tipo: "+Type+
" Data: "+Date+
" Firmware: "+Firmware+
" Sala: "+room+
" Equipamento: "+equipment+
" Calibração: "+calibration+
" Próxima Calibração: "+next_calibration+
" RF: "+rf+
" Bateria: "+battery+"<br>").appendTo('#page-wrap');
});
}
});
});
</script>
以下是我用来显示整个文档的代码
这是xml文件
< ?xml version="1.0" encoding="iso-8859-1"?>
<modulos>
<modulo>
<MAC>foobar</MAC>
<Type>foobar</Type>
<Date>foobar</Date>
<Firmware>foobar</Firmware>
<room>foobar</room>
<equipment>foobar</equipment>
<calibration>foobar</calibration>
<next_calibration>foobar</next_calibration>
<rf>foobar</rf>
<battery>foobar</battery>
</modulo>
<modulo>
<MAC>foobar</MAC>
<Type>foobar</Type>
<Date>foobar</Date>
<Firmware>foobar</Firmware>
<room>foobar</room>
<equipment>foobar</equipment>
<calibration>foobar</calibration>
<next_calibration>foobar</next_calibration>
<rf>foobar</rf>
<battery>foobar</battery>
</modulo>
</modulos>
答案 0 :(得分:0)
像这样创建全局对象......
var myRecords ={
MAC: "",
Type: "",
Date: "",
Firmware: "",
room: "",
equipment: "",
calibration: "",
next_calibration: "",
rf: "",
battery: ""
};
myFunction (){
$.ajax({
type: "GET",
url: "modulos.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('modulo').each(function(index){
myRecords.Mac[index] = $(this).find("Mac").text();
myRecords.Type[index]= $(this).find("Type").text();
myRecords.Date[index] = $(this).find("Date").text();
.
. and so on
}
}
所以当你想打印所有Mac的记录时,就像说
for(var val in myRecords){
alert(myRecords[val].Mac);
}
我希望这能回答你