Javascript-XML加载jQuery
我在使用jQuery将XML数据加载到javascript时遇到问题 我这里有一个xml:Javascript-XML加载jQuery,javascript,jquery,xml,Javascript,Jquery,Xml,我在使用jQuery将XML数据加载到javascript时遇到问题 我这里有一个xml: <config> <device> <node>1</node> <name>Block</name> <description>Block in saloon</description> <area>Salon</area> </device>
<config>
<device>
<node>1</node>
<name>Block</name>
<description>Block in saloon</description>
<area>Salon</area>
</device>
<device>
<node>2</node>
<name>Line</name>
<description>Lottr</description>
<area>Living room</area>
</device>
</config>
我应该在var kurs里放什么 类似于这个问题:
var myVal;
$(xml).find('node').each( //find the node and loop through them
function(){
var node = $(this);
if (node.text==="2") { //see if the node's value is 2
myVal = node.siblings("name").text(); //find the sibling name element
return false; //exit the each loop
}
}
);
console.log(myVal);
我认为这样做应该奏效:
$.ajax({
type: "GET",
url: "config2.xml",
dataType: "xml",
success: function(xml) {
//this should get you the device node
var $kurs = $(xml).find('node:contains("2")').parent();
//this should get you the name from the device node
var name = $kurs.find('name');
}
});
}) 我不明白你的问题。你想在库尔人身上放什么?为什么?到底是什么问题?正在加载文件吗?
$.ajax({
type: "GET",
url: "config2.xml",
dataType: "xml",
success: function(xml) {
//this should get you the device node
var $kurs = $(xml).find('node:contains("2")').parent();
//this should get you the name from the device node
var name = $kurs.find('name');
}
});
$(document).ready(function () {
$.ajax({
type: "GET",
url: "config2.xml",
dataType: "xml",
success: function (xml) {
$(xml).find('device').each(function () {
var node = $(this).find('node');
if (node.text() == '2') {
name = $(this).find('name').text();
}
});
}
});