简体   繁体   English

如何通过.append()在dom中显示json对象数组

[英]How to display an array of json objects in the dom via .append()

So I have an array that's being pulled from a json object that I'd like to display as images in a div called #practice: 所以我有一个数组要从一个json对象中提取,我想将其显示为一个名为#practice的div中的图像:

["<img src ="http://domain.com/wp-content/uploads/2012/10/random04-128x84.jpg"/>", 

"<img src ="http://domain.com/wp-content/uploads/2012/10/chuck_norris_random_fact_generator_6_3957_2224_image_2578-128x84.jpg"/>"]

Here is the method I have so far... 这是我到目前为止的方法...

$.getJSON('wp-content/themes/invoke-dmd/get-client-logos.php/', function (data) {
    var items = [];
    $.each(data, function(key, val) {
        items.push('<img src ="' + val.url + '"/>');
    });
  console.log(items)
  $('#practice').append(items);
});

It logs out the array as above... but cannot show it in the div. 它如上所述退出数组...但是无法在div中显示它。

How does one do this? 如何做到这一点?

您尝试将图像HTML添加为数组,需要添加一个字符串:

$('#practice').append(items.join(" "));

Try this, 尝试这个,

$.getJSON('wp-content/themes/invoke-dmd/get-client-logos.php/', function (data) {

    $.each(data, function(key, val) {
        $('#practice').append('<img src ="' + val.url + '"/>');
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM