【问题标题】:jQuery read new XML depending on first XMLjQuery 根据第一个 XML 读取新的 XML
【发布时间】:2011-02-04 00:49:53
【问题描述】:

我有工作在 http://fiddle.jshell.net/gaby/UC2dM/2/show/ 的工作 jQuery 代码并读取 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>

【问题讨论】:

  • 可以包含你的jquery代码吗?

标签: jquery jquery-ui


【解决方案1】:

您只需添加如下内容:

<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

【讨论】:

  • 我已经添加了Jquery代码,请检查并告诉我需要更改的内容...
【解决方案2】:

您可以在CategoryToUl方法中进行更改,在链接生成时

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-23
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多