最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery appending '[object, Object]' rather than image - Stack Overflow

programmeradmin0浏览0评论

I am using the $.getJSON function of JQuery to download the urls of some images and I am trying to output them inside a div. I am trying to get the output to look like this:

<a href="the image url (shot.short_url)"><img src="the direct image url (shot.image_teaser_url)" /></a>

However, it is outputting this instead:

<div id="body-wrapper">
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
    <a href="">[object Object]</a>
</div>

Please can you tell me where I am going wrong in the case out outputting the image?

This is my code:

function work() {
            $('#body-wrapper').empty();
            $.getJSON(".json?callback=?", function(data){
                $.each(data.shots, function(i,shot){
                  var image = $('<img/>').attr('src', shot.image_teaser_url);
                  var title = '<a href=\"' + shot.short_url + '\">';
                  var string = title;
                  string = string + image;
                  string = string + '</a>';
                  $('#body-wrapper').append(string);
                });
              });
        }

I am using the $.getJSON function of JQuery to download the urls of some images and I am trying to output them inside a div. I am trying to get the output to look like this:

<a href="the image url (shot.short_url)"><img src="the direct image url (shot.image_teaser_url)" /></a>

However, it is outputting this instead:

<div id="body-wrapper">
    <a href="http://drbl.in/300896">[object Object]</a>
    <a href="http://drbl.in/298080">[object Object]</a>
    <a href="http://drbl.in/290395">[object Object]</a>
    <a href="http://drbl.in/290324">[object Object]</a>
    <a href="http://drbl.in/268595">[object Object]</a>
    <a href="http://drbl.in/265197">[object Object]</a>
    <a href="http://drbl.in/256368">[object Object]</a>
    <a href="http://drbl.in/252519">[object Object]</a>
    <a href="http://drbl.in/242235">[object Object]</a>
    <a href="http://drbl.in/241676">[object Object]</a>
</div>

Please can you tell me where I am going wrong in the case out outputting the image?

This is my code:

function work() {
            $('#body-wrapper').empty();
            $.getJSON("http://dribbble./jakekrehel/shots.json?callback=?", function(data){
                $.each(data.shots, function(i,shot){
                  var image = $('<img/>').attr('src', shot.image_teaser_url);
                  var title = '<a href=\"' + shot.short_url + '\">';
                  var string = title;
                  string = string + image;
                  string = string + '</a>';
                  $('#body-wrapper').append(string);
                });
              });
        }
Share Improve this question asked Oct 24, 2011 at 23:17 max_max_ 24.5k41 gold badges125 silver badges214 bronze badges 2
  • 2 It's an object not a string. Dereference it to the url value. – Joe Commented Oct 24, 2011 at 23:20
  • Thanks man, I'm using var image = '<img src=\"' + shot.image_teaser_url + '\" />'; now. – max_ Commented Oct 24, 2011 at 23:23
Add a ment  | 

2 Answers 2

Reset to default 3

image is a jQuery object and not a String - so appending it to a String will produce [object Object]

Ideally, change everything into an object - e.g.

$('#body-wrapper').append(
    $("<a/>",{"href": shot.short_url}).append(
    $("<img/>",{"src": shot.image_teaser_url}));

or cheat and do this string = string + image.html();

Either should work

Note: I typed those without syntax checking and there's a LOT of brackets, did my best tho!

Try this within your .each() callback

// create image
var image = $('<img>').attr('src', shot.image_teaser_url);

// create anchor and append image
var anchor = $('<a>').attr('href', shot.short_url).append(image);

// append anchor to container
$('#body-wrapper').append(anchor);
发布评论

评论列表(0)

  1. 暂无评论