2011-02-03 8 views
1

http://fiddle.jshell.net/gaby/UC2dM/2/show/에서 작동하는 Working jQuery 코드가 있으며 XML을 읽습니다.jQuery는 첫 번째 XML에 따라 새 XML을 읽습니다.

이제 누군가가 뉴스, 기사, 대상 (예 : 예 :)을 클릭하면 해당 XML과 해당 데이터를 읽어야합니다.

예를 들어, 뉴스 XML은 "data_96.xml"기사 "data_97.xml"를 읽어 것 등등 ... data_catid.xml의

일반화 된 형식이

<record> 
    <recordId>251</recordId> 
    <title>Czech Sandstone Chalk Ban Lifted</title> 
    <author>|</author> 
    <published>2010-01-20T14:36:00.000-08:00</published> 
    <origUrl>http://www.rockandice.com/news/358-Czech-Sandstone-Chalk-Ban-Lifted</origUrl> 
    <numComments>0</numComments> 
    <data>&lt;![CDATA[&lt;p&gt;According to a report on czechclimbin</data> 
</record> 

처럼 보이는 것 여기


내 JQuery와 코드

입니다 .. 제가이 XMLS을 읽기 위해 jQuery 코드를 변경하는 방법을 알려 주시기 바랍니다
<script type='text/javascript'> 
    //<![CDATA[ 
    $(window).load(function(){ 

    $.ajax({ 
    url:'data.xml', 
    dataType: 'xml', 
    type:'post', 
    success: function(data){ 
     var xml = $(data); 
     $('#container').append(CategoryToUl(xml.children())); 

    } 
}); 

function CategoryToUl(xml){ 
    var categories = xml.children('category'); 
    if (categories.length > 0) 
    { 
     var ul = $('<ul/>'); 
     categories.each(function(){ 
      var $this = $(this); 
      var li = $('<li/>'); 
      var a = $('<a/>',{ 
       text: $this.children('title').text(), 
       href: '#' + $this.children('catId').text() 
      }); 
      li.append(a); 
      li.append(CategoryToUl($this)); 
      ul.append(li); 
     }); 
     return ul; 
    } 
    return null; 
} 

    }); 
    //]]> 
    </script> 

</head> 
<body> 
    <div id="container"></div> 

</body> 
당신은
+0

jquery 코드를 포함 할 수 있습니까? – Victor

답변

0

이런 식으로 뭔가를 추가 :

var a = $('<a/>',{ 
    text: $this.children('title').text(), 
    href: '#', 
    'data-id': $this.children('catId').text(), 
    class='categoryrecord' 
}); 

에 대한 링크 생성에,

<script type='text/javascript'> 
    //<![CDATA[ 
    $(window).load(function(){ 

$('a').live('click', function() { 
    var catId = $(this).attr('href').replace('#', '');  
    //here you can use $.get to get your new xml file. 
    //construct the url using the catid 
    //you would then parse the xml to display it. 
}); 

//here is the rest of your code 
+0

Jquery 코드를 추가했습니다. 변경해야 할 내용을 확인하고 알려주십시오. – user580950

+0

내 대답 편집 – Victor

0

당신은 CategoryToUl 방법에 변화를 만들 수 $(window).load(function(){에서 추가

$('a.categoryrecord').live('click',function(e){ 
    e.preventDefault(); 
    var cid = $(this).data('id'); 
    $.ajax({ 
     url:'data_' + cid + '.xml', 
     dataType: 'xml', 
     type:'post', 
     success: function(data){ 
      var xml = $(data); 
      // .. do what you want here with the xml 
     } 
    }); 
}); 
관련 문제