我正在尝试使用JavaScript设置<img>
src
,但视觉上缺少图像;它是空的。运行此代码后的图片网址以"http://127.0.0.1:8080/%27"
(我甚至不知道它来自哪里)而不是"data: image/png"
开头。变量mySrc
设置为Base64编码的图像。
document.getElementById(id).src="'"+mySrc+"'";
这是设置后的URL:
http://127.0.0.1:8080/%27data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4RMNRXhpZgAASUkqAAgAAAAQAAABAwABAAAAuAsAAAEBAwABAAAA9gkAAAIBAwADAAAAzgAAAAMBAwABAAAAAQAAAAYBAwABAAAAAgAAAA4BAgAfAAAA1AAAABIBAwABAAAAAAAAABUBAwABAAAAAwAAABoBBQABAAAA9AAAABsBBQABAAAA/AAAABwBAwABAAAAAQAAACgBAwABAAAAAgAAADEBAgALAAAABAEAADIBAgAUAAAAEAEAADsBAgAEAAAAUm9uAGmHBAABAAAAJAEAAHwBAAAIAAgACABJbWFnZSBjb252ZXJ0ZWQgdXNpbmcgaWZmdG9hbnkAAMDGLQAQJwAAwMYtABAnAABQaWNhc2EgMy4wAAAyMDA4OjA2OjA2IDEwOjM2OjEyAAQAAaADAAEAAAD...
答案 0 :(得分:2)
您应该设置src
不带撇号:
document.getElementById(id).src=mySrc;