当我想使用for循环更改源图像并切换大小写以检测图像对象的ID时,我有一个src属性为undefined
的代码。这是我的代码。
function buildTable(results) {
var data = results.data;
let gbr;
for (i = 0; i < data.length; i++) {
var c = "<canvas id='can" + i + "' class='printCanvas' ></canvas><br>";
$("body").append(c);
canvas2[i] = new fabric.Canvas('can' + i + '');
canvas2[i].setWidth(298);
canvas2[i].setHeight(420);
if (data[i] != "") {
var row = data[i];
var cells = row.join(",").split(",");
// console.log(cells);
}
canvas2[i].loadFromJSON(canvas.toJSON(['id', 'qrcode']), function() {}, function(o, object) {
switch (object.id) {
case 'id':
object.text = cells[1];
break;
case 'daerah':
object.text = cells[3];
break;
case 'tgl':
object.text = cells[2];
break;
case 'qrcode':
gbr = object;
object.setSrc(makeQR(cells[1]), canvas.renderAll());
break;
default:
break;
}
console.log(gbr.src);
});
canvas2[i].renderAll();
}
}
错误原因是由以下代码引起的
console.log(gbr.src);