2009-06-11 75 views
0

我正在使用jQuery将HTML shell插入页面,并用XML填充shell。这是有问题的XMLjQuery嵌套的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> 

因此,首先,我做的是进口的HTML代码段为每个minorPropCategory,并使用此代码

$(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); 
         }); 

的问题是,符合 “$(本).find( 'minorProp'))。每个(函数({” 不火,因为 “这” 已经改变了。我想,通过现在,我的小白被显示。我觉得我在错误的方式这样做。任何帮助,将不胜感激。谢谢。

下面发布的是什么,我试图做完整的.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

在运行之前解决这个问题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(){ 
     ... 
    } 

    ... 
} 

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