请告诉我如何使用 jquery 或 javascript 获取页面中所有图像的图像大小
最佳答案
您可以获得的唯一尺寸是可见尺寸。 clientWidth和 clientHeight是执行此操作的两个 DOM 属性。示例:
var image = document.getElementById("id");
var width = image.clientWidth;
var height = image.clientHeight;
如果您使用 jQuery,则只需使用 $.width和 $.height :
var width = $("id").width();
var height = $("id").height();
因此,要获取所有图像的大小,请循环它们:
$("img").each(function()
{
console.log(this.width());
console.log(this.height());
});
如果您需要实际尺寸,请看这个问题Get the real width and height of an image with JavaScript? (in Safari/Chrome)
关于javascript - 使用 JQuery/javascript 获取图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4552324/