使用jquery更改DIV顺序 [英] Change DIV order with jquery

查看:1231
本文介绍了使用jquery更改DIV顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理客户端的基于HTML的网站,并需要在刷新页面时随机排列一组Div。我通常通过PHP和一个数据库调用处理这个,但它是一个静态网站。

I'm working on a client's HTML based site and need to randomly order a set of Divs on refresh of a page. I would normally handle this through PHP and a database call, but it's a static site.

所以,我想知道是否有人知道如何随机显示一组div使用jquery?

So, I'm wondering if anyone knows how to randomly display a set of div's using jquery?

这是一个例子:

<div class="myItems">
     <div class="item">1</div>
     <div class="item">2</div>
     <div class="item">3</div>
</div>

,刷新时可能会更改为:

and on refresh, it might change to:

<div class="myItems">
     <div class="item">2</div>
     <div class="item">3</div>
     <div class="item">1</div>
</div>

任何人都知道如何做?

推荐答案

其实很简单:

$(".myItems").html($(".myItems .item").sort(function(){
    return Math.random()-0.5;
}));

就是这样!
享受。

That's it! Enjoy.

这篇关于使用jquery更改DIV顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆