如何在JavaScript或jQuery中按尺寸对图像进行排序

4
我该如何在JavaScript或jQuery中按尺寸对图像进行排序。我的代码如下:
var imgsrc = '';
if (document.images.length < 1) {
    alert('No images to open');
    return;
}
for (var i = 0; i < window.parent.document.images.length; i++) {
    imgsrc + = '\n';
}

if (imgsrc != '') {
    var newwin = window.open('', '');
    newwin.document.open();
    newwin.document.write('\n' + imgsrc + '');
    newwin.document.close();
} else {
    alert('No images!')
}​

请帮我一下,非常感谢。
2个回答

2

点击这里了解如何获取图像尺寸。您可以像这样对数组进行排序。.sort()有一个重载函数,可以传入一个排序函数。在那里,您可以比较大小。


0
如果你正在使用jQuery:
​var images = $('img');

if ( images.length ) {
// images were found
 images.sort( function (img1, img2) { return img1.width - img2.width } );                                     
}

这里有一个可用的 fiddle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接