我如何在javascript或jquery中按维度对图像进行排序

我如何在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!') }​ 

请帮帮我。 先感谢您。

请参阅此处获取图像尺寸。 您可以像这样对数组进行排序。 .sort()有一个带有sort函数的重载。 那是你比较大小的地方。

如果你使用jquery:

 ​var images = $('img'); if ( images.length ) { // images were found images.sort( function (img1, img2) { return img1.width - img2.width } ); } 

这是一个工作小提琴