【问题标题】:Returning only the first node when parsing XML using jQuery使用 jQuery 解析 XML 时仅返回第一个节点
【发布时间】:2014-01-15 22:52:46
【问题描述】:

我希望我的 jQuery 脚本只返回它找到的顶部节点,而不是遍历所有节点。我要在我的 jQuery 脚本中进行哪些更改来实现这一点?我知道这与each(function) {} 有关,但我不知道应该改变什么。提前致谢!

jQuery 代码:

$.ajax({
url: 'xml/timeline.xml',
dataType: 'xml',
success: function(data) {
    $(data).find('channel item').each(function() {
        var fbFeedWrapper = $('<div/>').addClass('fb-feed-wrapper');
        var fbFeedItem = $('<div />').addClass('fb-feed-item');
        $('.socialmedia-list#simlearn').append(fbFeedWrapper);
        fbFeedWrapper.append(fbFeedItem);

        var url = $(this).find('link').text();
        var title = $(this).find('title').text();           
        fbFeedItem.append($('<h1/>').html("<a href='" + url + "'>" + title + "</a>"));                
        var fbItemIcon = $('<div />').addClass('fb-item-icon');
        fbFeedItem.append(fbItemIcon);
        var description = $(this).find('description').text();
                        if (description.length > 80) {
                            description = description.substr(0, 80) + "...";
                        }
        fbFeedItem.append($('<div/>').addClass('fb-item-details').html(description));                            

    })
}


});

XML 文件格式:

<channel>
<item> // <-- If i Only want the top <item> node returned what should i do?
    <title></title>
    <link></link>
    <description></description>
</item>
<item>
    <title></title>
    <link></link>
    <description></description>
</item>
<item>
    <title></title>
    <link></link>
    <description></description>
</item>

【问题讨论】:

    标签: jquery xml


    【解决方案1】:

    而不是使用.each();,只需找到第一个匹配的元素。使用此对象分配一个变量,然后将 .each() 内部的 this 引用更改为变量的名称。

    示例(在示例中我将变量命名为self):

    换行:

    $(data).find('channel item').each(function() {
    

    成为:

    var self = $(data).find('channel item').first();
    

    然后将所有出现的this 更改为self。还要去掉.each函数(}))的结束大括号和括号

    $.ajax({
        url: 'xml/timeline.xml',
        dataType: 'xml',
        success: function (data) {
            var self = $(data).find('channel item').first();
            var fbFeedWrapper = $('<div/>').addClass('fb-feed-wrapper');
            var fbFeedItem = $('<div />').addClass('fb-feed-item');
            $('.socialmedia-list#simlearn').append(fbFeedWrapper);
            fbFeedWrapper.append(fbFeedItem);
    
            var url = $(self).find('link').text();
            var title = $(self).find('title').text();
            fbFeedItem.append($('<h1/>').html("<a href='" + url + "'>" + title + "</a>"));
            var fbItemIcon = $('<div />').addClass('fb-item-icon');
            fbFeedItem.append(fbItemIcon);
            var description = $(self).find('description').text();
            if (description.length > 80) {
                description = description.substr(0, 80) + "...";
            }
            fbFeedItem.append($('<div/>').addClass('fb-item-details').html(description));
        }
    });
    

    【讨论】:

      【解决方案2】:

      试试这个:

      $(data).find('channel item[0]')
          //--------------------^^^-----add [0] to this
      

      :first:

      $(data).find('channel item:first')
      

      :eq():

      $(data).find('channel item:eq(0)')
      

      你也可以试试这个:

      $(data).find('item:eq(0)')
      

      【讨论】:

      • 当我尝试用$(data).find('channel item[1]') 替换$(data).find('channel item').each(function() { 时,jquery 不会执行
      • $(data).find('channel item:eq(0)') 为我工作,但 $(data).find('channel item[0]') 不适合我
      猜你喜欢
      • 2021-05-24
      • 2020-09-20
      • 2020-12-23
      • 2023-03-13
      • 2015-05-21
      • 1970-01-01
      • 2013-03-31
      • 2021-03-20
      • 1970-01-01
      相关资源
      最近更新 更多