寻找一种获取外部图像尺寸(宽度和高度)的方法。
我使用了 prop()
和 attr()
,但它们不返回任何值。只是一个错误。
示例:
<a href="pathtotheimage">some link</a>
最佳答案
jQuery
var path = 'https://source.unsplash.com/random/600x300/?montreal';
$("<img/>").attr('src', path).load(function() {
console.log(this.width, this.height);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
普通 Javascript
var path = 'https://source.unsplash.com/random/600x300/?montreal';
var img = new Image();
img.src = path;
img.addEventListener('load', function() {
console.log(this.width, this.height);
});
关于javascript - jQuery:如何获取外部图像的尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10338613/