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

发布于 2024-11-02 10:17:12 字数 2301 浏览 5 评论 0原文

我讨厌一直问基本问题,看起来像个十足的白痴,但在我坐下来掌握 Javascript 和 jQuery 之前,我必须依赖你们这里善良而慷慨的人们。

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

代码:

/*
    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();
});

一如既往,任何帮助都会得到我无尽的感激。

I hate to keep asking basic questions and looking like a complete idiot, but until I can sit down and master Javascript and jQuery, I have to rely on you kind and generous folks here.

I'm calling Tumblr posts tagged "_featured" and showing them in the sidebar along with an image if it's a text post with an included image. The only problem is that the image itself isn't linked. Not a big deal, but a little unintuitive. How would I fix that?

And the code:

/*
    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();
});

As always, any help receives my undying gratitude.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-11-09 10:17:12

您的问题是,当您希望图像位于 内时,您将图像附加到

  • 中。 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);
    

    应该可以解决问题。这些更改在

  • 内构建 ,然后将图像放在 < 的前面(即前置) /代码>。您可能需要稍微调整 CSS 才能获得您想要的布局,可能类似于:
  • a     { display: block; }
    a img { display: block; }
    

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

    Your problem is that you're appending the image to the <li> when you want it inside the <a> that will be inside that <li>. Changing your list item builder from this:

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

    to something like this:

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

    should do the trick. The changes build the <a> inside the <li> and then put the image at the front (i.e. prepends) of the <a>. You'll probably have to adjust the CSS a little bit to get the layout you want, probably something like:

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

    with the appropriate CSS selectors to localize them.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文