簡體   English   中英

使用JQuery / javascript獲取圖像大小

[英]Get image size using JQuery/ javascript

請告訴我如何使用jquery或javascript獲取頁面中所有圖像的圖像大小

您可以獲得的唯一大小是可見大小。 clientWidthclientHeight是執行此操作的兩個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());
});

如果需要實際尺寸,請參閱此問題。 使用JavaScript獲取圖像的實際寬度和高度嗎? (在Safari / Chrome中)

$('img').each(function() {
   $(this).width(); // current image's width
   $(this).height(); // current image's height
});

請告訴我如何使用jquery或javascript獲取頁面中所有圖像的圖像大小

您可以使用jQuery的width()height()方法:

$('img').each(function(){
   alert('width=' + $(this).width() + '\nHeight=' + $(this).height());
});

更多信息:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM