牛骨文教育服务平台(让学习变的简单)
博文笔记

jquery 获取图片真实大小的两种方法

创建时间:2016-01-11 投稿人: 浏览次数:2080

第一种:

$("<img/>") // Make in memory copy of image to avoid css issues

    .attr("src", $(""#imgid").attr("src"))

    .load(function() {

        pic_real_width = this.width;   // Note: $(this).width() will not

        pic_real_height = this.height; // work for in memory images.

  

    });

第二种:

var theImage = new Image(); 

theImage.src = $(""#imgid").attr( "src"); 

alert( "Width: " + theImage.width); 

alert( "Height: " + theImage.height); 


第一种要安全性要高些,保证是在图片加载完成后在获取。

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。