0

I am using jquery to pull in a XML feed. The problem is that all the information comes in under a single node:

    <description><![CDATA[<div class="element element-date first">
    Friday, 09 August 2013</div>
<div class="element element-textarea">
    <p>CES MMA Champion Mike "The Beast" Campbell (13-4, Providence, RI.), Dinis "Sweetbread" Paiva (E. Providence, RI.), Keith "Sonic Boom" Jeffrey (9-2, Pawtucket, RI.) and more!</p></div>
<div class="element element-link last">
    <a href="http://www.ticketmaster.com/ces-mma-presents-live-cagefighting-lincoln-rhode-island-08-09-2013/event/01004ACBB23C9DC4?artistid=1765605&majorcatid=10004&minorcatid=830" title="Click here to buy tickets!" target="_blank" >Click here to buy tickets!</a></div>]]></description>

I can pull this information in and get it to display just fine. What I need to do is pull a very specific part of this node, The date:

<div class="element element-date first">
        Friday, 09 August 2013</div>

I am not sure on how to get this information so I will post the jquery code I am working on and see if anyone can help me figure out a solution:

$(document).ready(function () {
    var i = 0;
    var title = [];
    var temp = [];
    var desc = [];
    var $divElements = $('.element element-date first');

    $.ajax({
        type: "GET",
        url: "http://twinriver.com/index.php/entertainment/twin-river-event-center/feed/rss/events/event-center?format=feed",
        dataType: "xml",
        success: function (xml) {
            $(xml).find('item').each(function () {
                title[i] = $(this).find('title').text();
                desc[i] = $(this).find('description').text();
                i++;
                //alert(i);
            });
            //this is where im trying to get it to pull the info i need.
            for (var x = 0; x < i; x++) {
                alert($(desc[x]).find($divElements).val());
            }
            for (var t = 0; t < i; t++) {
                //      
                $('#EventList').append('<span class="title">' + title[t] + '</span>' + '<br />' + '<br />' + '<span>' + desc[t] + '<br />' + '<hr />');
            }
        }
    });
});

Update 07/03/2013:

Been trying to use the solutions presented by the replies with no luck. The alert comes up with one of two items, its blank or reads "undefined". I have also tried using something like the following:

eDate[i] = $(this).find('description').find('.element.element-date.first').html();

            alert(eDate[i]);

I added the var eDate = []; with the rest of the declared variables

Could it cause a problem the fact that the info im trying to get is not wrapped in a

tag or ? If so is there anyway around having to add them? I dont think I can really edit the incoming data format but I will also research this as an option.

4

4 回答 4

1
$('.element.element-date.first').each(function() {
  $(this).text(); // In each iteration this will get you the date
});

这样您就不需要编写自己的循环,(请参阅.each() 文档)您遇到的另一个问题是,当您看到 class="class1 class2 class3" 时,您在 jQuery 和 CSS 中使用 . 在每个类之前(因为在类属性中由空格分隔的值是功能不同的 CSS 类)。

于 2013-07-02T20:53:37.820 回答
0

你的选择器应该是这样的 -

var $divElements = $('.element.element-date.first');
于 2013-07-02T20:47:50.290 回答
0

将选择器传递给find而不是 jQuery 对象

var divSelector = '.element.element-date.first';
...
        for (var x = 0; t <= i; x++) {
            alert($(desc[t]).find(divSelector).text());//try also $.parseHTML(desc[t])
        }
于 2013-07-02T20:56:29.690 回答
0

主要问题在于您感兴趣的内容实际上包装在 CDATA 部分中,因此您不会在这些节点的 dom 树中获得层次结构,而只有一个节点代表 CDATA 内的整个文本. 此外,由于 CDATA 中的内容没有自己的根,因此在传递给 jquery 时不会评估为 xml 结构,这一事实使情况变得复杂。

一个简单的解决方案是将 text() 结果从

desc[i] = $(this).find('description').text();

像这样:

desc[i] = "<rootnode>" + $(this).find('description').text() + "</rootnode>";

然后警报将在将 for 循环表达式修复为后正确显示日期

for (var t = 0; t < i; t++)

并用一个简单的替换选择器。

var divElements = '.element.element-date.first';

可能还有其他更好的解决方案,但我对此进行了测试并且它有效。

更新:毛毡代码可以使用一些清洁。没有理由让所有这些数组在一些重组的情况下四处浮动。

$(document).ready(function () {
    $.ajax({
    type: "GET",
    url: "http://twinriver.com/index.php/entertainment/twin-river-event-center/feed/rss/events/event-center?format=feed",
    dataType: "xml",
    success: function (xml) {
      $(xml).find('item').each(function () {
            var title = $(this).find('title').text();
            var desc = "<rootnode>" + $(this).find('description').text() + "</rootnode>";
            var date = $(desc).find('.element.element-date.first').text()
            $('#EventList').append('<span class="title">' + title + '</span>' + '<br />' + '<br />' + '<span>' + date + '<br />' + '<br />');
            });
        }
    });
});
于 2013-07-03T19:48:34.773 回答