我正在使用 JQuery 的 $.getJSON
函数下载一些图像的 url,并尝试将它们输出到一个 div 中。我试图让输出看起来像这样:
<a href="the image url (shot.short_url)"><img src="the direct image url (shot.image_teaser_url)" /></a>
但是,它输出的是:
<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>
请你告诉我在输出图像的情况下我哪里出了问题?
这是我的代码:
function work() {
$('#body-wrapper').empty();
$.getJSON("http://dribbble.com/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);
});
});
}
最佳答案
image 是一个 jQuery 对象而不是 String - 所以将它附加到 String 会产生 [object Object]
理想情况下,将一切都变成一个对象——例如
$('#body-wrapper').append(
$("<a/>",{"href": shot.short_url}).append(
$("<img/>",{"src": shot.image_teaser_url}));
或者欺骗并做到这一点 字符串 = 字符串 + image.html();
两者都可以
注意:我在没有语法检查的情况下输入了这些内容,并且有很多括号,我已经尽力了!
关于javascript - JQuery 附加 '[object, Object]' 而不是图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7883058/