2010-05-30 1 views
0

하나 도와주세요.xml 추가 문제

나는 2 단계의 xml을가집니다. 예 :

제 생각에는 "list.each"를 사용하여 ul + index에 추가합니다. 그것은 잘 작동합니다. 그리고 내 문제는 내가 "list.each"를 추가하는 동안, "sublistgroup"은 "list.each"에 추가해서는 안되며, "sublistgroup"은 "ul"을 만들 필요가 있고 ul에서는 "sublist"가 필요합니다. 어린이는 ..

내 코드는 여기에

...

내가 pls는 하나 그것을 수정하고 알려 .. 내가 어떤 길을 잘못하고있는 중이 야한다는 konw ...는

$(function(){ 
    $.get('career-utility.xml',function(myData){ 

    $(myData).find('listgroup').each(function(index){ 
      var count = index; 
      var listGroup = $(this); 
      var listGroupTitle = $(this).attr('title'); 
      var shortNote = $(this).attr('shortnote'); 
      var subLink = $(this).find('sublist'); 
      var firstList = $(this).find('list'); 

      $('.grouplist').append('<div class="list-group"><h3>'+listGroupTitle+'</h3><ul class="level-one level' + count + '"></ul></div>'); 

      firstList.each(function(listnum){ 
       var subList = $(this).text(); 

       var subListLeveltwo = $(this).find('sublist').text(); 

       if(subListLeveltwo==''){ 
        $('<li>'+subList+'</li>').appendTo('ul.level'+count+''); 
       } 
       else{ 
        $('<li class="new">'+subList+'</li>').appendTo('ul.level'+count+''); 
       } 

      }) 

    }) 



    })  
}) 
+0

입력 및 예상 출력 XML 문서의 예를 제공 할 수 있습니까? –

+0

질문하신 내용에 대한 답변을 수락하지 않았습니다. 돌아가서 대답 옆에있는 체크 표시를 클릭하여 도움이되었던 대답을 수락해야합니다. 질문을 검토하려면 여기를 클릭하십시오. http://stackoverflow.com/users/218349/3gwebtrain – user113716

답변

0

여기에 기존의 XML 구조를 다시 사용하는 다른 솔루션입니다.

조금 짧습니다.

$.get('career-utility.xml',function(myData){ 

    $(myData).find('listgroup').each(function(index){ 

     var count = index; 
     var listGroup = $(this); 
     var listGroupTitle = $(this).attr('title'); 
     var shortNote = $(this).attr('shortnote'); 
     var subLink = $(this).find('sublist'); 
     var firstList = $(this).find('list'); 

     $('.grouplist').append('<div class="list-group"><h3>'+listGroupTitle+'</h3><ul class="level-one level' + count + '"></ul></div>'); 

     firstList.each(function(listnum) { 
      // This simply wraps the content of existing XML nodes, 
      // then unwraps the old node 
      $(this).wrapInner('<li>') 
        .find('sublistgroup').wrapInner('<ul>').children().unwrap() 
        .find('sublist').wrapInner('<li>').children().unwrap(); 

       // Append content of 'list' node 
      $('ul.level'+count).append($(this).children()); 

     }); 
    }); 
}); 

적은 코드로 동일한 결과를 산출합니다.

0

나는 가정 sublistgroup을 발견하면 중첩 된 <ul> 목록을 만들려고합니다.

$.get('career-utility.xml',function(myData){ 

    $(myData).find('listgroup').each(function(index){ 

     var count = index; 
     var listGroup = $(this); 
     var listGroupTitle = $(this).attr('title'); 
     var shortNote = $(this).attr('shortnote'); 
     var subLink = $(this).find('sublist'); 
     var firstList = $(this).find('list'); 

     $('.grouplist').append('<div class="list-group"><h3>'+listGroupTitle+'</h3><ul class="level-one level' + count + '"></ul></div>'); 

      // Cache the current UL for performance 
     var $currentUL = $('ul.level'+count); 

     firstList.each(function(listnum) { 

      var $subListGroup = $(this).find('sublistgroup').remove(); 

      var listHeading = $(this).text(); 

      $currentUL.append('<li>'+listHeading+'</li>'); 

      if($subListGroup.length){ 

        // Cache the new UL for the sub group, and append to the currentUL 
       var $subUL = $('<ul class="sublevel' + count + '"></ul>').appendTo($currentUL); 

       $subListGroup.children('sublist').each(function() { 
        $subUL.append('<li>' + $(this).text() + '</li>') 
       }); 
      } 
     }); 
    }); 
}) 

는 다음과 같은 HTML을 생성해야 :

그렇다면,이 시도

<div class="grouplist"> 
    <div class="list-group"> 
     <h3>Lifestyle</h3> 
     <ul class="level-one level0"> 
      <li>Type of Company: Architects may be self-employed.</li> 
      <li>Workspace &ndash; Indoors/outdoors: Architects work both.</li> 
      <li>Environment</li> 
      <ul class="sublevel0"> 
       <li>Travel: Architects often visit construction sites to review the progress of projects.</li> 
       <li>People: They work a lot with other professionals involved in the construction project including engineers, contractors, surveyors and landscape architects.</li> 
       <li>Casual: They usually work in a casual and comfortable environment.</li> 
       <li>Hours: The hours are varied based on the project they are working on.</li> 
       <li>Physically demanding: They stand on their feet.</li> 
       <li>Tools: Computers - Architects </li> 
      </ul> 
      <li>Assist clients in obtaining construction bids</li> 
      <li>Observe, inspect and monitor building work</li> 
     </ul> 
    </div> 
</div>​ 
+0

도움을 주셔서 감사합니다. 내가 먼저 구현하자. – 3gwebtrain