将 space 添加到使用 jQuery 从 RSS 获取的元素

Add space to elements got from RSS with jQuery

我正在尝试通过从 RSS 获取标签来更改 Tumblr 帖子 class。

我已经拿到标签了:

<script type="text/javascript">
    var rssurl = '/tagged/ExampleTag/rss';
    $.get(rssurl, function(data) {
    $('#content').append('<div class="example_post">');
    var $xml = $(data);
    var vari = 0;
        $xml.find("item").each(function() {
            var $this = $(this),
                item = {
                    link: $this.find("link").text(),
                    category: $this.find("category").text(),
                    description: $this.find("description").text()
            }
            vari = vari +1;
            if(vari <3){
            $('#content').append('<div class="example_post ' + item.category + '" style="overflow:hidden;"><div class="featured_description' + vari + '">' + item.description + '</div><div class="example_link"><a href="' + item.link + '">Example</a></div></div>');

            }
        });
        $('#content').append('</div>');
    });
</script>

问题是当我将 class 添加到 "example_post" class ( item.category ) 时,如果它有多个类别,则没有 space 在 class 之间。

示例:

标签是 "travel" - 这就是我得到的

<div class="example_post travel"> ...

标签是 "travel, inspiration & life" -

<div class="example_post travelinspirationlife"> ...

我想要的是得到这个:

<div class="example_post travel inspiration life"> ...

抱歉解释不当,在此先感谢您的帮助。

您需要遍历 category 个标签并使用空格将它们相互连接起来

$xml.find("item").each(function() {
    var $this = $(this);
    var categories = '';
    $this.find("category").each(function(){
      categories +=$(this).text()+" ";
    });
    item = {
        link: $this.find("link").text(),
        category: $.trim(categories),
        description: $this.find("description").text()
    }
    vari = vari +1;
    if(vari <3){
    $('#content').append('<div class="example_post ' + item.category + '" style="overflow:hidden;"><div class="featured_description' + vari + '">' + item.description + '</div><div class="example_link"><a href="' + item.link + '">Example</a></div></div>');

    }
});