2009-06-11 76 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(){ 
     ... 
    } 

    ... 
} 

這將保留對嵌套函數中正確對象的引用。