0

图像源拥有真实的图像尺寸(宽度/高度)。在一个div#pics有 5 个图像的图像中,3 个图像是 16X16,2 个图像是 2x2。两个 2x2 图像都有一个应用到它们的类,使它们成为 16x16,其他 3 个图像是 16x16。问题是,当我以这种方式过滤时:

var Images = $('#pics img');
var FilteredImages = Images.filter(function(){
    return ($(this).width() == 16) || ($(this).height() == 16)
});

FilteredImages包含所有 5 个图像,因为它考虑了使 2 个 2x2 图像为 16x16 的类。如何FilteredImages只保存其源图像大小(不是 css)为 16x16 的 3 个图像?

4

3 回答 3

2

我建议:

var Images = $('#pics img'),
    FilteredImages = Images.filter(function(){
        var that = this;
        return (that.naturalWidth == '16px') || (that.naturalHeight == '16px')
    });

参考:

于 2012-10-15T21:15:39.003 回答
1
$('#pics img[width=16][height=16]').css({border:'1px solid red'});
于 2012-10-15T21:15:48.260 回答
0

您可以将图像添加到临时对象以获取其原始大小:

function getImgSize(imgSrc) {
  var newImg = new Image();

  newImg.onload = function() {
    var height = newImg.height;
    var width = newImg.width;
    alert ('The image size is '+width+'*'+height);
  }

  newImg.src = imgSrc; // this must be done AFTER setting onload

}

在此处查看答案: 确定图像跨浏览器的原始大小?

于 2012-10-15T21:17:36.077 回答