如何按高度对 DIV 进行排序?
我有三个 div
,我想按高度从最大到最小对它们进行排序。
<div>smallest</div>
<div>largest</div>
<div>middle</div>
有什么想法吗?
I have three div
s and I want to sort them by height, from largest to smallest.
<div>smallest</div>
<div>largest</div>
<div>middle</div>
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这很简单。使用
.sort()
:代码:http://jsfiddle.net/fEdFt/2/
It's quite simple. Use
.sort()
:Code: http://jsfiddle.net/fEdFt/2/
由于 jQuery 返回一个数组,因此您可以在数组上使用
sort
方法对元素重新排序。一旦它们按照正确的顺序排序,您就可以从 DOM 中删除它们并按照所需的顺序重新插入它们。Since jQuery returns an array, you can use the
sort
method on array to reorder the elements. Once they are sorted in the proper order, you can then remove them from the DOM and reinsert them in the order desired.我认为 http://james.padolsey.com/javascript/sorting-elements- with-jquery/ 可能会为您提供一个很好的起点。
I think http://james.padolsey.com/javascript/sorting-elements-with-jquery/ might provide you a good starting point.
我不记得 jQuery 是否会包装常规数组对象,但如果没有,您可以使用选择器来获取 div,然后使用 Array.sort(sortFunc) 对它们进行排序。
I don't recall if jQuery will wrap the regular array object or not, but if not you can use a selector to get the divs and then use Array.sort(sortFunc) to sort them.
其 html 是:
The html for this was: