如何在 JQuery 中自动更新数组?

发布于 2024-11-03 21:01:04 字数 769 浏览 1 评论 0原文

我试图将一个项目添加到数组的末尾,有点像数组列表。我尝试使用 .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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲜肉鲜肉永远不皱 2024-11-10 21:01:04

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文