javascript - 如何显示 JSON 数据中的图像 URL?

标签 javascript jquery json

我创建了一个 HTML 页面,在表中显示 JSON 数组数据。该数组包括 ID、标题和图像。它看起来很棒,但图像当前只是文本,当我需要它来显示链接的实际图像时。我不知道如何解决这个问题。

HTML:

<div id="container">
  <div id="one_article" style="">
    <table id="article_table" class="table table-bordered ">
      <th>ID</th>
      <th>Title</th>
      <th>Cover Image</th>
    </table>
  </div>
</div>

Javascript:

$(document).ready(function() {
  $.getJSON("article.json", function(data){
    console.log(data) //just to log in console as well
    var article_data = '';
    $.each(data, function(key, value){
      article_data += '<tr>';
      article_data += '<td>'+value.id+'</td>';
      article_data += '<td>'+value.title+'</td>';
      article_data += '<td>'+value.cover+'</td>';
      article_data += '</tr>';
    });
    $('#article_table').append(article_data);
  });
});

当前查看:

enter image description here

最佳答案

使用img标签显示图像:

$(document).ready(function() {
  $.getJSON("article.json", function(data){
    console.log(data) //just to log in console as well
    var article_data = '';
    $.each(data, function(key, value){
      article_data += '<tr>';
      article_data += '<td>'+value.id+'</td>';
      article_data += '<td>'+value.title+'</td>';
      article_data += '<td><img src="'+value.cover+'"></td>';
      article_data += '</tr>';
    });
    $('#article_table').append(article_data);
  });
});

关于javascript - 如何显示 JSON 数据中的图像 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45619367/

相关文章:

javascript:根据重复项拆分多维数组

javascript - 我可以使用 rx.js 观察数组的添加吗?

javascript - 加载 gif 图像未在 IE 和 chrome 中显示

jquery - 如何让 jquery .closest() 在这种特殊情况下工作?

Char* 从 char 数组中解析错误

javascript - 使用ajax(不使用jQuery)发送php数组会在服务器上产生错误数据

javascript - jQuery 剪贴板插件不适用于动态添加的元素

javascript - 显示一位数百分比,跳过固定数字vue

javascript - 原型(prototype) JSON 到对象

javascript - 在内容完全加载之前不显示页面