javascript - 使用 JavaScript 从 JSON 获取图像并放入 html div 中

标签 javascript html json

我有一些 JSON 数据,我想从中获取图像并以某种方式将其放入 div 中,我尝试了以下代码,但它不起作用。

JSON

var data={"articles":
    [
  {
        "id": 1,
        "title": "Lorem Ipsum is simply dummy",
        "sub": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text.",
        "auth": "Latin text",
        "image": "http://placehold.it/350x150"
      },
        {
        "id": 2,
        "title": "Lorem Ipsum is simply dummy",
        "sub": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text.",
        "auth": "Latin text",
        "image": "http://placehold.it/350x150"
      },
      {
        "id": 3,
        "title": "Lorem Ipsum is simply dummy",
        "sub": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text.",
        "auth": "Latin text",
        "image": "http://placehold.it/350x150"
      },
      {
        "id": 4,
        "title": "Lorem Ipsum is simply dummy",
        "sub": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text.",
        "auth": "Latin text",
        "image": "http://placehold.it/350x150"
  }
]}

JAVASCRIPT

data.articles.forEach( function(obj) {
  var img = new Image();
  img.src = obj.image_url;
document.getElementById("image_url_1").innerHTML=data.articles[1].image_url;
});

HTML(图像进入单独的 div id,如下所示:

<div id="image_url_1"></div>
<div id="image_url_2"></div>

图像需要放在单独的 div 中。我不知道我做错了什么?

最佳答案

我建议采用以下方法:

// using 'let' to declare variables, rather than 'var'
// if you require compatibility with ES5 then use
// 'var' instead:
let data = {
    // content of JSON removed for brevity
    "articles": [...]
},

 // creating an <img> element:
 image = document.createElement('img'),

 // defining the id prefix (the portion that
 // appears before the index):
 idPrefix = 'image_url_',

 // declaring empty variables for later use,
 // rather than repeatedly declaring them
 // within the loop:
 receivingElement,
 imageClone;

// iterating over the data.articles Array, using
// Array.prototype.forEach():    
data.articles.forEach(function( obj, index ){
  // obj:   the first argument, a reference to the current
  //        Object of the Array of Objects over which we're
  //        iterating,
  // index: the zero-based index of the current Object within
  //        the Array over which we're iterating.

  // finding the element to which the <img> should be appended,
  // using document.getElementById(), passing it the concatenated
  // String of the prefix plus the index + 1 (your <div> element
  // ids begin at 1, whereas JavaScript indices are zero-based;
  // so we add the 1 to compensate for that difference:
  receivingElement = document.getElementById( idPrefix + (index + 1) );

  // if the element exists (and the value of the variable is
  // therefore not null):
  if (receivingElement) {

    // we clone the created <img>:
    imageClone = image.cloneNode();

    // we set the src of that cloned <img> to
    // the property-value of the obj.image
    // property-value:
    imageClone.src = obj.image;

    // appending the cloned <img> to the receivingElement:
    receivingElement.appendChild( imageClone );
  }
});

let data = {
    "articles": [{
      "id": 1,
      "title": "Lorem Ipsum is simply dummy",
      "sub": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text.",
      "auth": "Latin text",
      "image": "http://placehold.it/350x150"
    }, {
      "id": 2,
      "title": "Lorem Ipsum is simply dummy",
      "sub": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text.",
      "auth": "Latin text",
      "image": "http://placehold.it/350x150"
    }, {
      "id": 3,
      "title": "Lorem Ipsum is simply dummy",
      "sub": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text.",
      "auth": "Latin text",
      "image": "http://placehold.it/350x150"
    }, {
      "id": 4,
      "title": "Lorem Ipsum is simply dummy",
      "sub": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text.",
      "auth": "Latin text",
      "image": "http://placehold.it/350x150"
    }]
  },
  image = document.createElement('img'),
  receivingElement,
  idPrefix = 'image_url_',
  imageClone;

data.articles.forEach(function(obj, index) {
  receivingElement = document.getElementById(idPrefix + (index + 1));
  if (receivingElement) {
    imageClone = image.cloneNode();
    imageClone.src = obj.image;
    receivingElement.appendChild(imageClone);
  }
});
<div id="image_url_1"></div>
<div id="image_url_2"></div>

关于javascript - 使用 JavaScript 从 JSON 获取图像并放入 html div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41194272/

相关文章:

javascript - 一次点击事件中的jquery多次点击事件

javascript - 我的网页如何判断用户是否放大了 View ?

javascript - 有没有办法知道溢出: hidden?时div的内容是否处于 "clipped"状态

c# - 在具有多个表的 Linq 查询中使用递归层次结构树结构并返回一些 Json 值

php - 如何在mysql json数组数据中写SELECT查询

javascript - 使用 jquery 显示/隐藏时不是预期的 'effect'

javascript - 由于未正确检测到怪癖模式,jQuery 1.10.2 的简单模式无法在 IE 中工作

c# - 在 JSON 通用属性序列化中包含类名

html - 可滚动 div 中的滚动条不会一直延伸到底部/右侧

javascript - 根据所选选项更改表单的可见性