<img src="images/business-card.webp" onerror="this.onerror=null; this.src='images/business-card.jpg' " class="m-auto img-fluid">
我想从js文件中编写这个功能。我该怎么做?
首先,在javascript中创建图像:
var image = document.createElement("IMG");
document.body.appendChild(image);
image.src = "images/business-card.webp;
image.class = "m-auto img-fluid";
现在,添加一个onerror函数:
blah.onerror = function(){this.onerror=null; this.src='images/business-card.jpg'};