简体   繁体   English

使用base64数据在JavaScript中设置背景图片

[英]Setting background image in javascript with base64 data

I'm having trouble setting the background image of a div using a data url. 我在使用数据URL设置div的背景图片时遇到问题。 Here's my code, which doesn't throw an error but also doesn't work: 这是我的代码,它不会引发错误,但是也不起作用:

profilePhotoData= shout.get("authorProfPic");

src = 'data:image/png;base64, ' + profilePhotoData.base64

outerDiv.style.backgroundImage="url("+src+")";

I know this css works: 我知道这CSS的作品:

body {
  background: #ffffff url("data:image/jpg;base64,/9j/4...") no-repeat top left;
}

Thus maybe you should: 因此,也许您应该:

  • try the background property. 尝试背景属性。
  • add the " between your url brackets 在网址括号之间添加

Not sure you can do that, you can try to do it with classes instead. 不确定是否可以这样做,可以尝试使用类代替。

Have a look Here 在这里看看

从base64字符串中删除换行符(\\ r \\ n),它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM