我正在尝试通过其属性获取xml节点.
编辑
我正在尝试使用javascript xml而不是jquery通过其属性值检索元素.有一个简单的方法吗?
如何从字符串搜索的多个实例中检索多个索引?
var str = "food";
var index1 = str.search("o"); // 1
var index2 = str.search("o"); // ?
Run Code Online (Sandbox Code Playgroud)
谢谢,温
有人可以告诉我为什么我的对象img不接受它在ajax调用之外的值吗?我还使用了构造函数来创建对象,但是这个函数也没有用.并且xml解析的值有效,我已经测试过了.如果我在成功事件中移动警报(img.location),将显示正确的值,但它不会在ajax函数之外.
请帮忙...
function getImage(){
img = new Object();
$.ajax({
type: "GET",
url: "hvimage.xml",
dataType: "xml",
success: function(xmlData){
var randImageId = Math.floor(Math.random()*3);
$(xmlData).find("image").each(function(index, e){
if(index == randImageId){
img.id = $(this).attr("id");
img.location = $(this).find("location").text();
img.answer = $(this).find("answer").text();
}
});
},
error: function(xmdData){
alert("error");
}
});
alert("test");
alert(img.location); //Keep getting undefined here..
}
Run Code Online (Sandbox Code Playgroud)
再次感谢,
德恩