使用jQuery解析XML时仅返回第一个节点

使用jQuery解析XML时仅返回第一个节点,jquery,xml,Jquery,Xml,我希望jQuery脚本只返回它找到的顶级节点,而不是遍历所有节点。为了实现这一点,我在jQuery脚本中做了哪些更改?我知道这和每个(函数){}有关,但我不知道应该改变什么。提前谢谢 jQuery代码: $.ajax({ url: 'xml/timeline.xml', dataType: 'xml', success: function(data) { $(data).find('channel item').each(function() { var fbFeedWr

我希望jQuery脚本只返回它找到的顶级节点,而不是遍历所有节点。为了实现这一点,我在jQuery脚本中做了哪些更改?我知道这和
每个(函数){}
有关,但我不知道应该改变什么。提前谢谢

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));                            

    })
}


});
$.ajax({
url:'xml/timeline.xml',
数据类型:“xml”,
成功:功能(数据){
$(数据)。查找('channel item')。每个(函数(){
var fbFeedWrapper=$('').addClass('fb-feed-wrapper');
var fbFeedItem=$('').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($(“”).html(“”);
var fbItemIcon=$('').addClass('fb-item-icon');
fbFeedItem.append(fbItemIcon);
var description=$(this.find('description').text();
如果(description.length>80){
description=description.substr(0,80)+“…”;
}
fbFeedItem.append($('').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>


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

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

更改行:

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

var self = $(data).find('channel item').first();
然后将所有出现的
this
更改为
self
。同时拆下
。每个
函数(
})
的结束大括号和括号

$.ajax({
url:'xml/timeline.xml',
数据类型:“xml”,
成功:功能(数据){
var self=$(数据).find('channel item').first();
var fbFeedWrapper=$('').addClass('fb-feed-wrapper');
var fbFeedItem=$('').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($(“”).html(“”);
var fbItemIcon=$('').addClass('fb-item-icon');
fbFeedItem.append(fbItemIcon);
var description=$(self.find('description').text();
如果(description.length>80){
description=description.substr(0,80)+“…”;
}
fbFeedItem.append($('').addClass('fb-item-details').html(description));
}
});

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

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

更改行:

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

var self = $(data).find('channel item').first();
然后将所有出现的
this
更改为
self
。同时拆下
。每个
函数(
})
的结束大括号和括号

$.ajax({
url:'xml/timeline.xml',
数据类型:“xml”,
成功:功能(数据){
var self=$(数据).find('channel item').first();
var fbFeedWrapper=$('').addClass('fb-feed-wrapper');
var fbFeedItem=$('').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($(“”).html(“”);
var fbItemIcon=$('').addClass('fb-item-icon');
fbFeedItem.append(fbItemIcon);
var description=$(self.find('description').text();
如果(description.length>80){
description=description.substr(0,80)+“…”;
}
fbFeedItem.append($('').addClass('fb-item-details').html(description));
}
});
试试这个:

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

$(data).find('channel item:first')
或使用
:eq()


即使您可以尝试以下方法:

$(data).find('item:eq(0)')
试试这个:

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

$(data).find('channel item:first')
或使用
:eq()


即使您可以尝试以下方法:

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

当我尝试替换
$(数据)时。find('channel item')。每个(function(){
替换
$(数据)。find('channel item[1]')
jquery不会执行
$(数据)。find('channel item:eq(0)
对我有效,但
$(数据)。find('channel item[0]')
在我尝试替换
$(数据)时对我无效。find('channel item')).each(function(){
$(data)。find('channel item[1]')
jquery不会执行
$(data)。find('channel item:eq(0)')
对我有效,但
$(data)。find('channel item[0]')
对我无效