Js下Image.src的坑

来源:互联网 发布:北京航空学院网络教育 编辑:程序博客网 时间:2024/05/19 16:27

先看看这个案例:(目前下面为部分代码)

var Img = new Image();

Img.src = _default.img;
//图片加载完成时
Img.onload = function(){

......

containerItem.css({
width : findWidth,
height : findHeight,
'background-image' : "url("+Img.src+")",
'background-size' : width+'px '+height+'px',
opacity : 0

});


以上这个代码中,_default.img为"./1.png",但是赋值过后,Img.src为绝对路径。但是在下面的使用中出现问题。由于绝对路径中,有英文的小括号,会出现问题导致图片读不出来。


所以下面引用时,用_default.img代替Img.src,解决此问题


0 0
原创粉丝点击