gpt4 book ai didi

jquery - 使用 jquery 在 XML 中查找元素

转载 作者:行者123 更新时间:2023-12-01 06:04:57 24 4
gpt4 key购买 nike

我正在使用 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])

但到目前为止还没有任何效果。有谁知道正确的语法是什么?

编辑:--------

<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>

最佳答案

像这样创建全局对象...

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);
}

我希望这能回答你

关于jquery - 使用 jquery 在 XML 中查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7282860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com