使用Tumblr API调用文本帖子中的图像:Part Deux

时间:2011-04-17 01:11:38

标签: javascript jquery image tumblr

我不想继续问基本问题,看起来像个完全白痴,但在我坐下来掌握Javascript和jQuery之前,我必须依靠你善良而慷慨的人。

我正在调用标记为“_featured”的Tumblr帖子并在侧边栏中显示它们以及图像(如果它是包含图像的文本帖子)。唯一的问题是图像本身没有链接。没什么大不了的,但有点不直观。我该如何解决这个问题?

代码:

/*
    TUMBLR FEATURED POSTS SCRIPT
    Automatically gets all posts tagged with "featured" and lists them
    REQUIRES JQUERY!
    --------------------------------------
    Created by james <at> bandit.co.nz
    http://blog.bandit.co.nz

    Some code borrowed from Jacob DeHart's AJAX Search:
    http://blog.bandit.co.nz/post/80415548/tumblr-ajax-inline-search
*/
Featured = {
    'apiNum' : 50, // how many posts to read
    'listId' : '_featured', // the id of the ul to write to
    'tagName' : '_featured', // the name of the tag we're searching for
    'linkAppend' : '', // html to append to the end of each linked post

    'postDB' : [],
    'listPos' : 0,
    'doList' : function (where) {
        var li; var ul = $('#'+where);
        var titles = {"link":"link-text", "photo":"photo-caption", "quote":"quote-text", "regular":"regular-title", "video":"video-caption"}

        // cycle through post database
        pcount = Featured.postDB.length;
        for(i=Featured.listPos;i<pcount;i++) {
            p = Featured.postDB[i];
            if(p[titles[p.type]] != '') titlestr = p[titles[p.type]].replace(/<\/?[^>]+>/gi, '');
            else titlestr = p['url'];

            li = document.createElement('li');
            $(li).append($(p['regular-body']).find('img')[0])
            .append('<div class="featurelink"><a class="'+p.type+'" href="'+p["url-with-slug"]+'">'+titlestr+Featured.linkAppend+'</a></div>');
            ul.append(li);

            Featured.listPos = pcount;
        }
    },

    'getData' : function() {
        $.get('/api/read/json?num='+Featured.apiNum+'&tagged='+Featured.tagName,
            function(data) {
                eval(data);
                for(i=0;i<tumblr_api_read.posts.length;i++) {
                    Featured.postDB.push(tumblr_api_read.posts[i]);
                    Featured.doList(Featured.listId);
                }
            }
        );
    }
};

$(document).ready(function(){
    Featured.getData();
});

与往常一样,任何帮助都会得到我不朽的感激之情。

1 个答案:

答案 0 :(得分:0)

您的问题是,如果您希望将图片附加到<li>内的<a>内,则会将图片附加到<li>。从这里更改列表项构建器:

li = document.createElement('li');
$(li).append($(p['regular-body']).find('img')[0])
     .append('<div class="featurelink"><a class="'+p.type+'" href="'+p["url-with-slug"]+'">'+titlestr+Featured.linkAppend+'</a></div>');
ul.append(li);

这样的事情:

$li = $('<li>');
$li.append('<div class="featurelink"><a class="' + p.type + '" href="' + p["url-with-slug"] + '">' + titlestr + Featured.linkAppend + '</a></div>');
$li.find('a').prepend($(p['regular-body']).find('img')[0]); // The important part
ul.append($li);

应该做的伎俩。更改会在<a>内构建<li>,然后将图像放在<a>的前面(即前置)。您可能需要稍微调整CSS以获得所需的布局,可能类似于:

a     { display: block; }
a img { display: block; }

使用适当的CSS选择器来本地化它们。