寻找一个javascript解决方案来重新排序div
我在页面中有一些div显示相同类型的不同内容,例如优惠,现在优惠有结束时间,还有发布时间,如果用户想按结束时间或发布时间排序,则应重新排序。
我正在寻找一个可以做到这一点的javascript解决方案,Ext JS或JQuery下的任何特定库都可以工作
这是这些div的样子
<div data-sortunit="1" data-sort1="40" data-sort2="156" data-sort3="1"
data-sort4="1317620220" class="item">
</div>
<div data-sortunit="2" data-sort1="30" data-sort2="116" data-sort3="5"
data-sort4="1317620220" class="item">
</div>
<div data-sortunit="3" data-sort1="10" data-sort2="157" data-sort3="2"
data-sort4="1317620220" class="item">
</div>
所以我希望能够根据data-sortN对这些div进行排序,N是一个整数
I have some divs in the page that show different things of the same kind, for example offers, now offers have ending time, and also posted time, if the user wants to order by ending time, or posted time, they should be re ordered.
I'm looking for a javascript solution that could do that, any particular libraries under Ext JS , or JQuery would work
Here is how these divs look like
<div data-sortunit="1" data-sort1="40" data-sort2="156" data-sort3="1"
data-sort4="1317620220" class="item">
</div>
<div data-sortunit="2" data-sort1="30" data-sort2="116" data-sort3="5"
data-sort4="1317620220" class="item">
</div>
<div data-sortunit="3" data-sort1="10" data-sort2="157" data-sort3="2"
data-sort4="1317620220" class="item">
</div>
So I wanna be able to sort these divs based on data-sortN, N being an integer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:好的,现在您已经提供了一些 HTML,下面是 javascript 代码,它将按所需的列号对特定的 HTML 进行排序:
您可以在 jsFiddle 中看到它的工作原理: http://jsfiddle.net/jfriend00/JG32X/
由于您没有给我们提供继续的 HTML,所以我制作了自己的 HTML并向您展示了如何使用 jQuery 进行排序:
HTML:
Javascript(加载页面后运行):
并且,一个 jsFiddle 显示它的实际操作: http://jsfiddle.net/jfriend00/vRdrA/。
Edit: OK, now that you've supplied some HTML, here's javascript code that will sort that specific HTML by the desired column number:
You can see it work here in a jsFiddle: http://jsfiddle.net/jfriend00/JG32X/
Since you've given us no HTML to go on, I've made my own HTML and shown you how you can use jQuery to sort:
HTML:
Javascript (run after page is loaded):
And, a jsFiddle that shows it in action: http://jsfiddle.net/jfriend00/vRdrA/.
我根据 jfriend00 的答案制作了一个小 jqueryPlugin:
您的 HTML:
用法:
容器的子元素可以是任何元素。唯一重要的是,它们是直接子级并且具有 data-X 密钥。
谢谢你,jfriend00!
I made a tiny jqueryPlugin out of jfriend00's answer:
Your HTML:
Usage:
The children of the container can be any Elements. Its only important, that they are immediate children and have data-X key.
Thank you, jfriend00!!