如何在 JQuery 中自动更新数组?
我试图将一个项目添加到数组的末尾,有点像数组列表。我尝试使用 .push() 但无法让它从我的 xml 中正确读取。我怎样才能“自动”更新jquery中的数组? (如果你想这样想,请附加到它的末尾)
这是我的 jquery 目前的样子。
images = new Array();
$.ajax({
type: "GET",
url: strXMLURL,
dataType: "xml",
success: function(xml) {
$(xml).find("image").each(function() {
images.push($(this).attr("src"));
});
},
error: function() {
alert("Error reading the XML");
}
});
xml
<?xml version="1.0" encoding="utf-8" ?>
<gallery>
<image src="images/gallery/gallery1.jpg"/>
</gallery>
如果您需要更多信息,我很乐意提供更多信息。
I am trying to add an item to the end of an array, kind of like an arraylist. I tried using the .push() but I can't get it working correctly reading from my xml. How could I "auto" update the array in jquery? (append to the end of it if you want to think of it that way)
Here is what my jquery looks like at the moment.
images = new Array();
$.ajax({
type: "GET",
url: strXMLURL,
dataType: "xml",
success: function(xml) {
$(xml).find("image").each(function() {
images.push($(this).attr("src"));
});
},
error: function() {
alert("Error reading the XML");
}
});
xml
<?xml version="1.0" encoding="utf-8" ?>
<gallery>
<image src="images/gallery/gallery1.jpg"/>
</gallery>
If you need any more information, I'll happily provide more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ajax 调用的 success 函数中声明图像数组,以确保范围保持正确,然后您的函数应该可以工作...
Declare the images array inside the success function of the ajax call to ensure the scope remains correct then your function should work...