随机分组 Jquery
我认为我之前的问题在标题和结果中可能都不清楚,所以我决定问这样的问题...
我有 3 个类别(A、B、C),在这些类别中我每个类别有 3 个项目。所以类别A)狗,猫,老鼠B)苹果,樱桃,香蕉C)红色,绿色,黄色。
我想知道是否可以将这些项目从类别中随机分组。因此,例如每次您单击同一个按钮时,它都会分组:dog、apple、yellow 或 cat、cherry、green 或 mouse、apple、red 等...并将类 .grouped 或其他内容添加到该组中。一次只有一个可能的组包含三个项目。
所以我想它会像从开始
class="dog"
class="cat"
class="mouse"
class="apple"
class="cherry"
class="banana"
class="red"
class="green"
class="yellow"
然后你单击按钮 &获取:
class="dog grouped"
class="apple grouped"
class="yellow grouped"
再次单击按钮&得到
class="cat grouped"
class="cherry grouped"
class="green grouped"
任何想法或建议,非常感谢
-大卫
I decided that my previous question wasnt perhaps clear in both title and result, so I decided to ask the question like this...
I have 3 categories (A,B,C) and in these categories i have 3 items each. So category A)dog,cat,mouse B)apple,cherry,banana C)red,green,yellow.
I was wondering if its possible to group these items from the categories at random. So for example each time you click on the same button it would group: dog,apple,yellow or cat,cherry,green or mouse,apple,red , etc... and adds the class .grouped or whatever to that group. with only one possible group at a time with three items.
so i guess it would be like from start
class="dog"
class="cat"
class="mouse"
class="apple"
class="cherry"
class="banana"
class="red"
class="green"
class="yellow"
and then you click button & get:
class="dog grouped"
class="apple grouped"
class="yellow grouped"
click button again & get
class="cat grouped"
class="cherry grouped"
class="green grouped"
any thoughts or suggestions greatly appreciated
-david
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会让你继续前进。对于html:
然后这个javascript将随机添加类
这个想法是在每个类的数量内生成一个随机数,然后将类添加到其中。
This will probably get you going. For the html:
Then this javascript will randomly add the class
The idea is to generate a random number within the number of each class, and then add the class to it.
正如您在下面看到的那样,这是可能的。肯定有更优雅的方法来做到这一点,但我认为这会很容易理解。
http://jsfiddle.net/EN7HQ/1/
JS
标记
Its possible as you can see below. There are definitely more elegant ways to do this, but I figured this would be pretty straight forward to understand.
http://jsfiddle.net/EN7HQ/1/
JS
Markup