我正在使用 jquery 加载图像:
var myImg = new Image();
$(myImg).load(function(){
console.log("image dimensions:"); // what should I put here ?
}).attr("src", "/path/toImg/");
加载 img 时,有没有办法获得它的宽度和高度?请注意,问题是我想在将 img 附加到正文之前获得此信息。
我正在使用 jquery 加载图像:
var myImg = new Image();
$(myImg).load(function(){
console.log("image dimensions:"); // what should I put here ?
}).attr("src", "/path/toImg/");
加载 img 时,有没有办法获得它的宽度和高度?请注意,问题是我想在将 img 附加到正文之前获得此信息。
它应该是:
$(myImg).load(function(){
var w = this.width;
var h = this.height;
...
}).attr("src", "/path/toImg/");
试试这个:
var curHeight;
var curWidth;
function getImgSize(imgSrc)
{
var newImg = new Image();
newImg.src = imgSrc;
curHeight = newImg.height;
curWidth = newImg.width;
}