2011-12-21 69 views
2

我試圖使用JavaScript設置<img>src,但圖像缺少可視化效果;它是空的。運行此代碼後的圖像URL以"http://127.0.0.1:8080/%27"(我甚至不知道這是從哪裏來的)開始,而不是"data: image/png"。變量mySrc被設置爲Base64編碼的圖像。設置<img> src轉換爲使用Javascript的Base64編碼圖像失敗

document.getElementById(id).src="'"+mySrc+"'"; 

這就是URL的樣子設置之後:

http://127.0.0.1:8080/%27data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/4RMNRXhpZgAASUkqAAgAAAAQAAABAwABAAAAuAsAAAEBAwABAAAA9gkAAAIBAwADAAAAzgAAAAMBAwABAAAAAQAAAAYBAwABAAAAAgAAAA4BAgAfAAAA1AAAABIBAwABAAAAAAAAABUBAwABAAAAAwAAABoBBQABAAAA9AAAABsBBQABAAAA/AAAABwBAwABAAAAAQAAACgBAwABAAAAAgAAADEBAgALAAAABAEAADIBAgAUAAAAEAEAADsBAgAEAAAAUm9uAGmHBAABAAAAJAEAAHwBAAAIAAgACABJbWFnZSBjb252ZXJ0ZWQgdXNpbmcgaWZmdG9hbnkAAMDGLQAQJwAAwMYtABAnAABQaWNhc2EgMy4wAAAyMDA4OjA2OjA2IDEwOjM2OjEyAAQAAaADAAEAAAD... 
+0

無從得知,沒有看到更多的代碼,比如如何定義並填充「mySrc」。 – Chad 2011-12-21 19:29:05

回答

2

您應該設置src沒有撇號:

document.getElementById(id).src=mySrc; 
+0

你是對的我把撇號,但在客戶端 – user592704 2011-12-21 19:37:07

+0

但是根本沒有主機文本;我檢查:( – user592704 2011-12-21 19:40:11

+0

什麼確切包含'mySrc'?聲明在哪裏? – abuduba 2011-12-21 19:55:44