如何使用 JavaScript 获取图像大小(高度和宽度)? [英] How to get image size (height & width) using JavaScript?

查看:31
本文介绍了如何使用 JavaScript 获取图像大小(高度和宽度)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何 JavaScript 或 jQuery API 或方法来获取页面上图像的尺寸?

Are there any JavaScript or jQuery APIs or methods to get the dimensions of an image on the page?

推荐答案

您可以使用 Javascript 以编程方式获取图像并检查尺寸...

You can programmatically get the image and check the dimensions using Javascript...

const img = new Image();
img.onload = function() {
  alert(this.width + 'x' + this.height);
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

如果图像不是标记的一部分,这会很有用.

This can be useful if the image is not a part of the markup.

这篇关于如何使用 JavaScript 获取图像大小(高度和宽度)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆