简体   繁体   English

单击jquery尝试设置背景图像:url(data:image; base64为div

[英]jquery on click trying to set background-image: url( data:image;base64 for div

Here http://jsfiddle.net/m3e1kdzu/9/ is what i am trying 这是我正在尝试的http://jsfiddle.net/m3e1kdzu/9/

First div is html with data:image;base64 第一个div是带有data:image;base64 html

<div id="large_photo_null" style="width:50px; height:50px; 
background-image: url( data:image;base64,R0lGODlhR..  );" >
</div>

With html i see background image as expected. 与html我看到预期的背景图像。

Now trying to set background image for another div. 现在尝试为另一个div设置背景图像。 But no changes. 但是没有变化。 Can not set 无法设定

Here is code 这是代码

<div id="large_photo" style="width:100px; height:150px;" >
This is as if large photo where expect to change background
</div>

<div class="thbn_photo" style="width:100px; height:150px;" >
As if thumbnail photo. Click here.
</div>

Here jquery 这里的jQuery

$(document).on('click', '.thbn_photo', function(){
alert('thbn_photo clicked');

$('#large_photo').css( 'background-image: url( data:image;base64,R0lGODlhRgAzAJEAAMr..  );' );  

});

What is wrong? 怎么了? My code is incorrect or with jquery can not set data:image;base64 ? 我的代码不正确或无法通过jquery设置data:image;base64

Now found similar topic Is there a way to set background-image as a base64 encoded image in javascript? 现在发现类似的主题是否有办法将背景图像设置为javascript中的base64编码图像?

Will check 会检查

After 2 hours of fiddling with this, this solution worked for me. 经过2小时的摆弄之后,此解决方案对我有用。

var img = new Image();
img.src = "data:image/jpeg;charset=utf-8;base64, " + profilepic;
$("#profilepict").css("background-image", "url('" + img.src + "')");

jQuery's .css() accepts arguments like so: jQuery的.css()接受如下参数:

$('div').css({'background':'#000'});

try restructuring your last line there and see if you get different behavior. 尝试在那儿重组最后一行,看看是否有不同的行为。

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

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