如何使用 base64 设置图像源
我想将图像源设置为 base64 源,但它不起作用:
I want to set the Image source to a base64 source but it does not work:
JSfiddle.net/NT9KB
<img id="img" src="" />
JavaScript
document.getElementById("img").src = "data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg=="
推荐答案
尝试使用 setAttribute
代替:
document.getElementById('img')
.setAttribute(
'src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
);
真正的答案:(并确保删除 base64 中的换行符.)
Real answer: (And make sure you remove the line-breaks in the base64.)
相关文章