簡體   English   中英

如何自動更新 JQuery 中的數組?

[英]How do I auto-update an array in JQuery?

我正在嘗試將一個項目添加到數組的末尾,有點像 arraylist。 我嘗試使用.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>

如果您需要更多信息,我很樂意提供更多信息。

Declare the images array inside the success function of the ajax call to ensure the scope remains correct then your function should work...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM