【问题标题】:jquery on click trying to set background-image: url( data:image;base64 for divjquery点击尝试设置背景图像:url(数据:图像;div的base64
【发布时间】:2015-02-08 17:40:42
【问题描述】:

这里http://jsfiddle.net/m3e1kdzu/9/是我正在尝试的

第一个 div 是带有data:image;base64的html

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

使用 html,我可以看到预期的背景图像。

现在尝试为另一个 div 设置背景图片。但没有变化。无法设置

这里是代码

<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>

这里是jquery

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

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

});

怎么了?我的代码不正确或者用jquery不能设置data:image;base64

现在发现类似话题Is there a way to set background-image as a base64 encoded image in javascript?

会检查

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    经过 2 个小时的摆弄,这个解决方案对我有用。

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

    【讨论】:

      【解决方案2】:

      jQuery 的 .css() 接受如下参数:

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

      尝试在那里重组你的最后一行,看看你是否会得到不同的行为。

      【讨论】:

      • background':'#000 一起工作。但是如何使用data:image;base64 而不是#000.... 会尝试。感谢您的想法
      • 这是预期的行为吗?我修改了jquery的最后一行以匹配我的,还删除了后台url末尾的;jsfiddle.net/m3e1kdzu/12
      猜你喜欢
      • 2017-08-28
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2022-07-11
      • 1970-01-01
      相关资源
      最近更新 更多