开发者社区> 问答> 正文

IE 中new Image()不能加载成功,求指教?报错

项目中定义一个图片加载方法,源码如下$.fn.CalImage=function(imgSrc,iwidth,iheight){
     var image=new Image();
     image.src=imgSrc;
     if(image.complete){
      var oImage=new Image();
      oImage.src=imgSrc;
      if(image.width>0 && image.height>0){
        if(image.width/image.height>= iwidth/iheight){
            if(image.width>iwidth){
             oImage.width=iwidth;   
             oImage.height=(image.height*iwidth)/image.width;   
            }else{
             oImage.width=image.width;     
             oImage.height=image.height;   
            }   
        }else{
            if(image.height>iheight){
             oImage.height=iheight;   
             oImage.width=(image.width*iheight)/image.height;           
            }else{
             oImage.width=image.width;     
             oImage.height=image.height;   
            }   
        }   
      }
      return oImage;
     }else{
      image.onload=function(){
       var oImage=new Image();
       oImage.src=imgSrc;
       if(image.width>0 && image.height>0){   
         if(image.width/image.height>= iwidth/iheight){   
             if(image.width>iwidth){     
              oImage.width=iwidth;   
              oImage.height=(image.height*iwidth)/image.width;   
             }else{   
              oImage.width=image.width;     
              oImage.height=image.height;   
             }   
         }else{   
             if(image.height>iheight){     
              oImage.height=iheight;   
              oImage.width=(image.width*iheight)/image.height;           
             }else{   
              oImage.width=image.width;     
              oImage.height=image.height;   
             }   
         }   
       }
       image.onload=null;
       return oImage;
      };
     }
 };

现在在另一个方法中调用它$.fn.preview = function(width,height){
  var oWidth=width?width:300;
  var oHeight=height?height:400;
  $(this).each(function(){
   $(this).hover(function(e){
    var xOffset = 10;
    var yOffset = 20;
    var w = $(window).width();
     console.log('开始执行图片浏览'); 
    var  oTemp=$(this).attr('data')?$(this).attr('data'):$(this).attr('src');
    var  oImage=$(this).CalImage(oTemp,oWidth,oHeight);
     console.log(oImage); 

}

在chrome中可以使用但是在IE中会报错


SCRIPT5007: 无法获取属性“src”的值: 对象为 null 或未定义
preview.js, 行73 字符5

 

求指教,纠结很久了!

 

展开
收起
爱吃鱼的程序员 2020-06-20 17:10:35 650 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    解决了没有啊,同样碰到了这蛋疼的问题

    2020-06-20 17:10:53
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载