我想将图像源设置为 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.)
这篇关于如何使用 base64 设置图像源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!