随机分组 Jquery

发布于 2024-10-20 05:04:40 字数 718 浏览 1 评论 0原文

我认为我之前的问题在标题和结果中可能都不清楚,所以我决定问这样的问题...

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦一生花开无言 2024-10-27 05:04:40

这可能会让你继续前进。对于html:

<span class="animal">dog</span>
<span class="animal">cat</span>
<span class="animal">mouse</span>
<span class="fruit">apple</span>
<span class="fruit">cherry</span>
<span class="fruit">banana</span>
<span class="colour">red</span>
<span class="colour">green</span>
<span class="colour">yellow</span>

然后这个javascript将随机添加类

function regroup() {
  //remove existing
  $(".grouped").removeClass("grouped");

  //the random Math.floor is just to generate a random number
  $(".animal:eq(" + (Math.floor(Math.random() * 4)) + ")").addClass("grouped");
  $(".fruit:eq(" + (Math.floor(Math.random() * 4)) + ")").addClass("grouped");
  $(".colour:eq(" + (Math.floor(Math.random() * 4)) + ")").addClass("grouped");
}

这个想法是在每个类的数量内生成一个随机数,然后将类添加到其中。

This will probably get you going. For the html:

<span class="animal">dog</span>
<span class="animal">cat</span>
<span class="animal">mouse</span>
<span class="fruit">apple</span>
<span class="fruit">cherry</span>
<span class="fruit">banana</span>
<span class="colour">red</span>
<span class="colour">green</span>
<span class="colour">yellow</span>

Then this javascript will randomly add the class

function regroup() {
  //remove existing
  $(".grouped").removeClass("grouped");

  //the random Math.floor is just to generate a random number
  $(".animal:eq(" + (Math.floor(Math.random() * 4)) + ")").addClass("grouped");
  $(".fruit:eq(" + (Math.floor(Math.random() * 4)) + ")").addClass("grouped");
  $(".colour:eq(" + (Math.floor(Math.random() * 4)) + ")").addClass("grouped");
}

The idea is to generate a random number within the number of each class, and then add the class to it.

巷雨优美回忆 2024-10-27 05:04:40

正如您在下面看到的那样,这是可能的。肯定有更优雅的方法来做到这一点,但我认为这会很容易理解。

http://jsfiddle.net/EN7HQ/1/

JS

var groupA = ["dog","cat","mouse"],
    groupB = ["apple", "cherry","bannana"],
    groupC = ["red","green","yellow"];

$("#clickme").click(function(){
    $("#groups").children().remove();
    var itemA = groupA[Math.round(Math.random()*2)],
        itemB = groupB[Math.round(Math.random()*2)],
        itemC = groupC[Math.round(Math.random()*2)];

    $("#groups").append("<div class='" + itemA + " grouped'>" + itemA + "</div>"); 
    $("#groups").append("<div class='" + itemB + " grouped'>" + itemB + "</div>"); 
    $("#groups").append("<div class='" + itemC + " grouped'>" + itemC + "</div>"); 
});

标记

<div id="clickme">Click Me</div>
<div id="groups">
</div>

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

var groupA = ["dog","cat","mouse"],
    groupB = ["apple", "cherry","bannana"],
    groupC = ["red","green","yellow"];

$("#clickme").click(function(){
    $("#groups").children().remove();
    var itemA = groupA[Math.round(Math.random()*2)],
        itemB = groupB[Math.round(Math.random()*2)],
        itemC = groupC[Math.round(Math.random()*2)];

    $("#groups").append("<div class='" + itemA + " grouped'>" + itemA + "</div>"); 
    $("#groups").append("<div class='" + itemB + " grouped'>" + itemB + "</div>"); 
    $("#groups").append("<div class='" + itemC + " grouped'>" + itemC + "</div>"); 
});

Markup

<div id="clickme">Click Me</div>
<div id="groups">
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文