我在 HTML 表格上显示图像时遇到问题。图像存储在 MongoDB 中。我可以使用 <img>
JavaScript 上的标签来显示我的图像?或者还有其他方法可以做到这一点吗?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>E-commerce Website</title>
<script>
function loadBasket(){
var basketArray;
var tableBody;
var tableHeader = "<tr><th>Product Image</th><th>Product Name</th><th>Price</th></tr>\n";
for(var i=0; i<basketArray.length; ++i){
tableBody += "<tr><td>" + "<img class='categories_img' src='"basketArray[i].image"'>" + "</td><td>" + basketArray[i].name + "</td><td>" + basketArray[i].price + "</td></tr>";
}
}
</script>
</head>
<body onload="loadBasket()">
<table id="basket_list"></table>
</body>
</html>
最佳答案
您的图像文件名部分缺少串联运算符。您需要将该行更改为以下内容:
tableBody += "<tr><td>" + "<img class='categories_img' src='"+basketArray[i].image+"'>" + "</td><td>" + basketArray[i].name + "</td><td>" + basketArray[i].price + "</td></tr>";
关于javascript - 我可以在 JavaScript 上使用 img 标签来显示存储在 MongoDB 中的图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49654133/