0

这是从 Facebook 提取图像并显示它们的“我的代码”。

但我想围绕 <a href="image.source" class="highslide" onclick="return hs.expand(this) >

请帮我输出:

<a href="image.source" class="highslide" onclick="return hs.expand(this)>
<img src"image.source" width="167" height="167">
</a>

原始代码:

<script type="text/javascript" src="underscore-min.js"></script>
<script type="text/javascript">
$.getJSON('http://graph.facebook.com/261512740659107/photos').then(function (response) {

    // 0-8, 0 returns the largest available images, 8 the smallest
    var imageIndex = 4;

    var images = _(response.data)
        .chain()
        .pluck('images')
        .pluck(imageIndex)
        .value();

    console.log(images);

    _(images).each(function (image) {
        $('#image-container').append(
        $('<img>').attr('src', image.source).attr('height', 167).attr('width', 167));
    });
});
</script>
4

1 回答 1

0

您可以使用 jQuery wrap() 函数将标签放在<a>标签周围<img>。只需使用 appendTo() 来获取新元素的实例,然后您就可以 wrap() 它。

$.getJSON('http://graph.facebook.com/261512740659107/photos').then(function (response) {

    // 0-8, 0 returns the largest available images, 8 the smallest
    var imageIndex = 4;

    var images = _(response.data)
        .chain()
        .pluck('images')
        .pluck(imageIndex)
        .value();

    _(images).each(function (image) {
        $($('<img>').attr({'src': image.source, 'height': 167, 'width': 167})).appendTo('#image-container').wrap('<a href="' + image.source + '" class="highslide" onclick="return hs.expand(this);" />');
    });
});
于 2013-06-03T01:09:59.923 回答