jQuery:如何获取外部图像的尺寸?

寻找获得外部图像的尺寸(宽度和高度)的方法。
我使用了prop()attr() ,但它们没有返回任何值。 只是一个错误。

例:

 some link 

 var imgSrc = "http://sofzh.miximages.com/javascript/here.jpg" var _width, _height; $("").attr("src", imgSrc).load(function() { _width = this.width; _height = this.height; }); 

看起来没有人提供一个有效的vanilla js答案。

 var img = document.createElement('img') img.src = 'http://sofzh.miximages.com/javascript/img.png' img.onload = function() { console.log( this.width ) console.log( this.height ) } 

这是一个jsfiddle: http : //jsfiddle.net/Ralt/VMfVZ/

这在技术上不是jQuery,但我会沿着以下路线前进:

 var image = new Image(), width, height; image.src = 'http://sofzh.miximages.com/javascript/heresmyimage.jpg'; width = image.width; height = image.height; 

然后,您可以使用widthheight访问这些值,例如alert('My image is ' + width + ' pixels accross.');