javascript - 如何从json中打印jquery中的图像

标签 javascript jquery json

我试试这个

$.each(json, function(i, mhs) {
      $('<tr/>', {
        html: [$('<td/>', {
          text: mhs.nim
        }), $('<td/>', {
          text: mhs.nama
        }), $('<td/>', {
          text: mhs.alamat
        }), $('<td/>', {
          text: document.write(mhs.foto)
        })]
      }).appendTo('#mhsTable tbody')
    })

但是mhs.foto不在表中

这是我的 json 数据,我将其放入数组中

[{"nim":"E1E113aaa","nama":"AAAAA","alamat":"Jl. aaaa","foto":"<img src=aaa.jpg width=100px height=100px>"},{"nim":"E1E113aaa","nama":"AAAAA","alamat":"Jl. aaaa","foto":"<img src=aaa.jpg width=100px height=100px>"}]

最佳答案

要将 HTML 插入到元素中,请使用 jQuery 的 html() 方法。 text() 将对数据进行编码,并且永远不应使用 document.write。试试这个:

var json = [{
  "nim": "E1E113aaa",
  "nama": "AAAAA",
  "alamat": "Jl. aaaa",
  "foto": "<img src=\"aaa.jpg\" width=\"100px\" height=\"100px\">"
}, {
  "nim": "E1E113aaa",
  "nama": "AAAAA",
  "alamat": "Jl. aaaa",
  "foto": "<img src=\"aaa.jpg\" width=\"100px\" height=\"100px\">"
}]


$.each(json, function(i, mhs) {
  $('<tr/>', {
    html: [$('<td/>', {
      text: mhs.nim
    }), $('<td/>', {
      text: mhs.nama
    }), $('<td/>', {
      text: mhs.alamat
    }), $('<td/>', {
      html: mhs.foto // note the use of 'html' here
    })]
  }).appendTo('#mhsTable tbody')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mhsTable">
  <tbody></tbody>
</table>

关于javascript - 如何从json中打印jquery中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329377/

相关文章:

jquery - 使用jquery下载文件自定义名称

javascript - 关于使用 Zurb 的 Foundation+AngularJS 的想法?

javascript - 查找具有 class 和 id 的元素

json - 从types.JSON.TEXT转换为字符串列表

ios - 如何在ios中获取并显示Json Array数据

javascript - 无法在javascript中转换unicode字符

javascript - jquery mobile : target page not refreshing when called by mobile. 使用重新加载页面更改页面:true

javascript - 如何使用 Lodash 流来组合不同数量的函数?

JavaScript:从文化设置中获取日期格式

SQL:如何从 postgresql 中的 json 返回第 n 个值