jQuery嵌套XML问题

时间:2009-06-11 19:41:40

标签: javascript jquery xml ajax

我正在使用jQuery将HTML shell插入到页面中,并使用XML填充shell。这是有问题的XML

  <minorPropCategory title="Test Title 1">
    <minorProp>FLV</minorProp>
    <minorProp>BLV</minorProp>
    <minorProp>RLV</minorProp>
  </minorPropCategory>
  <minorPropCategory title="Test Title 2">
    <minorProp>LAS</minorProp>
    <minorProp>ILV</minorProp>
    <minorProp>BIL</minorProp>
  </minorPropCategory>

首先,我要做的是为每个minorPropCategory导入一个HTML代码段,并使用此代码添加标题

$(xml).find('minorPropCategory').each(function(){
                        var minorHeader=$(this).attr("title");
                        var minorId=$(this).attr("title").replace(/ /g,'');
                        var minorModuleContainerCode = "minorModuleContainer.html";
                        //names the div with a unique ID
                        minorDiv = $("<div id='"+minorId+"minorModuleContainer' class='minorModuleGroupContainer'>");
                        //Sets loading message in case it takes longer than usual to load
                        minorDiv.html("Loading......");
                        //After minorModuleContainerCode.html code loads, starts populating module
                        minorDiv.load(minorModuleContainerCode,"t",
                            function(){
                                $("#"+minorId+"minorModuleContainer").find(".minorModuleHeader").html(minorHeader);

                            }
                        );
                        $("#minorModuleContainer").append(minorDiv);

然后,我想要做的是添加另一个HTML代码段,然后填充它。这是我遇到问题的地方。我可以这样试试

//Create the actual minor modules
                        $(this).find('minorProp').each(function(){
                            var minorPropCode = $(this).text();
                            var minorModuleCode = "minorModule.html";
                            minorModuleDiv = $("<div id='"+minorPropCode+"minorModule' class='minorModule'>");
                            minorModuleDiv.html("Loading......");
                            minorModuleDiv.load(minorModuleCode,"b",
                            function(){
                                $.ajax({
                                    type: "GET", url: minorPropCode+".xml", dataType: "xml", 
                                    success: function(xml3) {
                                        $("#"+minorPropCode+"minorModule").find(".minorPropLogo").attr(
                                            {
                                                src:$(xml3).find('smallImage').text(),
                                                alt:$(xml3).find('smallImageAlt').text()
                                            }
                                        );

                                    }
                                });
                            });
                            $("#"+minorId+"minorModuleContainer").append(minorModuleDiv);
                        });
                    });

但它永远不会出现在页面上,因为我不认为它是在适当的时间发射的。或者,我尝试将次要模块的创建移动到它的父级的.load函数中,但是我遇到了另一个问题。代码看起来像这样。

//MINOR MODULE CODE
                    $(xml).find('minorPropCategory').each(function(){
                        var minorHeader=$(this).attr("title");
                        var minorId=$(this).attr("title").replace(/ /g,'');
                        var minorModuleContainerCode = "minorModuleContainer.html";
                        //names the div with a unique ID
                        minorDiv = $("<div id='"+minorId+"minorModuleContainer' class='minorModuleGroupContainer'>");
                        //Sets loading message in case it takes longer than usual to load
                        minorDiv.html("Loading......");
                        //After minorModuleContainerCode.html code loads, starts populating module
                        minorDiv.load(minorModuleContainerCode,"t",
                            function(){
                                $("#"+minorId+"minorModuleContainer").find(".minorModuleHeader").html(minorHeader);
                                $(this).find('minorProp').each(function(){
                                    alert("minorPropFired");
                                    var minorPropCode = $(this).text();
                                    var minorModuleCode = "minorModule.html";
                                    minorModuleDiv = $("<div id='"+minorPropCode+"minorModule' class='minorModule'>");
                                    minorModuleDiv.html("Loading......");
                                    minorModuleDiv.load(minorModuleCode,"b",
                                    function(){
                                        $.ajax({
                                            type: "GET", url: minorPropCode+".xml", dataType: "xml", 
                                            success: function(xml3) {
                                                alert("test");
                                                $("#"+minorPropCode+"minorModule").find(".minorPropLogo").attr(
                                                    {
                                                        src:$(xml3).find('smallImage').text(),
                                                        alt:$(xml3).find('smallImageAlt').text()
                                                    }
                                                );

                                            }
                                        });
                                });
                            $("#"+minorId+"minorModuleContainer").append(minorModuleDiv);
                        });

问题是,带有“$(this).find('minorProp')的行。每个(function(){”因为“this”已经改变而不会触发。我想,到现在为止,我的菜鸟正在显示。我觉得我这样做是错误的。任何帮助都会受到赞赏。谢谢。

以下是我正在尝试做的完整.js文件。

// JavaScript Document<script language="JavaScript" type="text/javascript">
    $(document).ready(function(){
        //gets the code for the ad from the URL.  Using URL jQuery add-on to make this super-easy
        var adCode = $.url.param("adCode");
        if (adCode != null){
            //gets the ad code's XML file
            $.ajax({
                type: "GET", url: adCode+".xml", dataType: "xml", 
                success: function(xml) {
                    //Set the header image
                    $("#headerImg").attr(
                        {
                            src:$(xml).find('headerImage').text(),
                            alt:$(xml).find('headerImageAlt').text()
                        }
                    );
                    //set the header text
                    $("#headerText").html($(xml).find('adText').text());
                    //set the top image
                    $("#topImg").attr(
                        {
                            src:$(xml).find('topImage').text(),
                            alt:$(xml).find('topImageAlt').text()
                        }
                    );
                    //MAJOR MODULE CODE - This code reads all of the major modules, then adds a majorModule div, and populates it
                    //Gets all majorProps from ad XML
                    $(xml).find('majorProp').each(function(){
                        var propCode=$(this).text();
                        var majorModuleCode = "majorModule.html";
                        //names the div with a unique ID
                        div = $("<div id='"+propCode+"majorModule' class='majorModule'>");
                        //Sets loading message in case it takes longer than usual to load
                        div.html("Loading......");
                        //After majorModule.html code loads, starts populating module
                        div.load(majorModuleCode,"t",
                            function(){
                                //Get the XML for the prop, which contains prop specific stuff
                                $.ajax({
                                    type: "GET", 
                                    url: propCode+".xml", 
                                    dataType: "xml", 
                                    success: function(xml2) {
                                        $("#"+propCode+"majorModule").find(".propImg").attr(
                                            {
                                                src:$(xml2).find('smallImage').text(),
                                                alt:$(xml2).find('smallImageAlt').text()
                                            }
                                        );
                                        $("#"+propCode+"majorModule").find(".propLogoImg").attr(
                                            {
                                                src:$(xml2).find('smallLogo').text(),
                                                alt:$(xml2).find('name').text()
                                            }
                                        );
                                        $("#"+propCode+"majorModule").find(".viewCalendarLinkA").attr(
                                            {
                                                href:"https://www.harrahs.com/AvailabilityCalendar.do?propCode="+propCode+"&showHotDeal=Y"
                                            }
                                        );
                                        $("#"+propCode+"majorModule").find(".learnMoreLinkA").attr(
                                            {
                                                href:$(xml2).find('homeLink').text()
                                            }
                                        );
                                        $("#"+propCode+"majorModule").find(".propText").html(
                                                $(xml2).find('bodyText').text()
                                        );

                                    }
                                });
                                //Get the lowest rate for the prop
                                $.ajax({
                                    type: "GET", 
                                    url: "lowest_rate\\"+propCode+".xml", 
                                    dataType: "xml", 
                                    success: function(xml3) {
                                        $("#"+propCode+"majorModule").find(".roomRate").html(
                                                "$"+($(xml3).find('roomtype').filter(
                                                    function (index) {
                                                        return $(this).attr("lowest") == "Y";
                                                    }).text()).slice(0, -3)
                                        )
                                    }
                                });
                            }
                        );
                        $("#mainModuleContainer").append(div);
                    });
                    //MINOR MODULE CODE
                    $(xml).find('minorPropCategory').each(function(){
                        var minorHeader=$(this).attr("title");
                        var minorId=$(this).attr("title").replace(/ /g,'');
                        var minorModuleContainerCode = "minorModuleContainer.html";
                        //names the div with a unique ID
                        minorDiv = $("<div id='"+minorId+"minorModuleContainer' class='minorModuleGroupContainer'>");
                        //Sets loading message in case it takes longer than usual to load
                        minorDiv.html("Loading......");
                        //After minorModuleContainerCode.html code loads, starts populating module
                        minorDiv.load(minorModuleContainerCode,"t",
                            function(){
                                $("#"+minorId+"minorModuleContainer").find(".minorModuleHeader").html(minorHeader);

                            }
                        );
                        $("#minorModuleContainer").append(minorDiv);
                        //Create the actual minor modules
                        $(this).find('minorProp').each(function(){
                            var minorPropCode = $(this).text();
                            var minorModuleCode = "minorModule.html";
                            minorModuleDiv = $("<div id='"+minorPropCode+"minorModule' class='minorModule'>");
                            minorModuleDiv.html("Loading......");
                            minorModuleDiv.load(minorModuleCode,"b",
                            function(){
                                $.ajax({
                                    type: "GET", url: minorPropCode+".xml", dataType: "xml", 
                                    success: function(xml3) {
                                        $("#"+minorPropCode+"minorModule").find(".minorPropLogo").attr(
                                            {
                                                src:$(xml3).find('smallImage').text(),
                                                alt:$(xml3).find('smallImageAlt').text()
                                            }
                                        );

                                    }
                                });
                            });
                            $("#"+minorId+"minorModuleContainer").append(minorModuleDiv);
                        });
                    });



                }
            });
        }
    });

1 个答案:

答案 0 :(得分:1)

要在运行minorDiv.load之前解决此问题,请执行以下操作

var elem = $(this);
minorDiv.load(minorModuleContainerCode,"t", function(){
    $("#"+minorId+"minorModuleContainer").find(".minorModuleHeader").
        html(minorHeader);

    // replace $(this) with elem here
    elem.find('minorProp').each(function(){
        ...
    }

    ...
}

这将在嵌套函数中保留对正确对象的引用。