使用javascript,我如何将rss提要放入列表中并只显示其中的4个提要

using javascript how do i put rss feed into a list and show only 4 of them

本文关键字:javascript 显示 4个 列表 rss 使用      更新时间:2023-09-26

我把rss标题放在一个下拉框中,当点击每个标题时它们都会改变,但我希望页面上显示大约4个rss提要,所以我不需要下拉框。

$(document).ready(function()
{
    $.ajax({
        type: "GET",
        url: "rss_warriors.php",
        dataType: "xml",
        cache: false,
        success: parse_rss
    });
    function parse_rss(rss_feed)
    {
        console.log(rss_feed);
        $('#output').append('<select>');
        $(rss_feed).find("item").each(function()
        {
            $('select').append('<option value="' + 
            $(this).find('title').text() + '">' + 
            $(this).find('title').text() + '</option>');
        });
        line_record(0,rss_feed);
        $("select").on("change", function(evt) 
        {
            line_record( $("select option:selected").index(),rss_feed)
        });   
    } 
    function line_record(sel_index,rss_feed) 
    {
        var local_index = sel_index;
        var image_url;                                               
        var item_title;
        var item_description;
        var item_pubDate;
        image_url = $(rss_feed).find("item").eq(local_index).find("thumbnail").last().attr("url");
        item_title = $(rss_feed).find("item").eq(local_index).find("title").text();                               
        item_description = $(rss_feed).find("item").eq(local_index).find("description").text();
        item_pubDate = $(rss_feed).find("item").eq(local_index).find("pubDate").text();
        $("#img_warriors").empty();
        $("#txt_warriors").empty();
        $("#img_warriors").append("<img src='" + image_url + "'>");
        $("#txt_warriors").append("<span class='title_large'>" + item_title + "</span>");
        $("#txt_warriors").append("<p>" + item_description + "</p>");
        $("#txt_warriors").append("<p>" + item_pubDate + "</p>");
    }   
});

不确定这是否是你的意思,但关于呢

$(rss_feed).find("item").each(function(i) {
    if (i <=3) {
            $('select').append('<option value="' + 
            $(this).find('title').text() + '">' + 
            $(this).find('title').text() + '</option>');
    }
});