我从一个数据库获取数据,但是其中一个是图像文件路径,比如'~\images\food.png‘,但是我想把它放在一个img标记中,这样它就可以显示图像,而不是路径。请帮帮忙,提前谢谢。
$(document).ready(function () {
load();
});
function load() {
$.ajax({
type: "POST",
url: "ParentCategoryManagment.aspx/PopulateCategories",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
}
function AjaxSucceeded(respone) {
var json = jQuery.parseJSON(respone.d);
if (typeof oTable == 'undefined') {
oTable = $('#tblData').dataTable({
'aaData': json,
'aoColumns':
[
{ "mDataProp": "Id" },
{ "mDataProp": "Name" },
{ "mDataProp": "ImagePath" },
{ "mDataProp": null, "sDefaultContent": '<a class="edit" href="">Edit</a>' },
{ "mDataProp": null, "sDefaultContent": '<a class="delete" href="">Delete</a>' }
]
});
}
else {
oTable.fnClearTable(0);
oTable.fnAddData(json);
oTable.fnDraw();
}
}
function AjaxFailed(result) {
alert('Ajax Failed');
}发布于 2013-01-10 02:34:02
查看JSFIDDLE演示:http://jsfiddle.net/HPdVH/5/
您需要遍历每个图像路径并将其转换为图像。在您的示例中,它似乎总是第三个表列,所以您可以在通过ajax加载表后执行此操作:
$("tr").each(function(){
$(this).children("td")
.eq(2)
.html("<img src='" + $(this).html() + "'/>");
})https://stackoverflow.com/questions/14243518
复制相似问题