最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery, random div order - Stack Overflow

matteradmin14PV0评论

I have this jQuery and HTML /

    <div class="container">
    <div class="yellow"></div>
    <div class="red"></div>
    <div class="green"></div>
    <div class="blue"></div>
    <div class="pink"></div>
    <div class="orange"></div>
    <div class="black"></div>
    <div class="white"></div>
    </div>​
$("div.container div").each(function(){
    var color = $(this).attr("class");
    $(this).css({backgroundColor: color});
});

I am trying to randomise the order, so the div.container div is in any random position, meaning not the same position it started. and the div must remain within the div.container

I have tried / / and more functions I found on the net but non are working

​how can I get the div's to display in a random order

I have this jQuery and HTML http://jsfiddle.net/UgX3u/30/

    <div class="container">
    <div class="yellow"></div>
    <div class="red"></div>
    <div class="green"></div>
    <div class="blue"></div>
    <div class="pink"></div>
    <div class="orange"></div>
    <div class="black"></div>
    <div class="white"></div>
    </div>​
$("div.container div").each(function(){
    var color = $(this).attr("class");
    $(this).css({backgroundColor: color});
});

I am trying to randomise the order, so the div.container div is in any random position, meaning not the same position it started. and the div must remain within the div.container

I have tried http://jsfiddle.net/UgX3u/32/ http://jsfiddle.net/UgX3u/20/ and more functions I found on the net but non are working

​how can I get the div's to display in a random order

Share Improve this question edited Feb 24, 2012 at 18:56 Naftali 146k41 gold badges247 silver badges304 bronze badges asked Feb 24, 2012 at 18:53 Yusaf KhaliqYusaf Khaliq 3,39311 gold badges45 silver badges82 bronze badges 3
  • 1 It seems to work okay, once the typo's fixed (you had .addCass instead of .addClass): Updated JS Fiddle. – David Thomas Commented Feb 24, 2012 at 18:59
  • +1 sorry for being a nonce :(. – Yusaf Khaliq Commented Feb 24, 2012 at 19:01
  • 2 Absolutely! 'cause I've never done that! ...not ever... runs =) – David Thomas Commented Feb 24, 2012 at 19:11
Add a comment  | 

3 Answers 3

Reset to default 13

Try this:

http://jsfiddle.net/UgX3u/33/

$("div.container div").sort(function(){
    return Math.random()*10 > 5 ? 1 : -1;
}).each(function(){
    var $t = $(this),
        color = $t.attr("class");
    $t.css({backgroundColor: color}).appendTo( $t.parent() );
});

.sort is applied to jQuery like this:

$.fn.sort = [].sort

Since it doesn't perform like other jQuery methods, it isn't documented. That does mean it is subject to change, however I doubt it will ever change. To avoid using the undocumented method, you could do it like this:

http://jsfiddle.net/UgX3u/37/

var collection = $("div.container div").get();
collection.sort(function() {
    return Math.random()*10 > 5 ? 1 : -1;
});
$.each(collection,function(i,el) {
    var color = this.className,
        $el = $(el);
    $el.css({backgroundColor: color}).appendTo( $el.parent() );
});
var $container = $("div.container");
$container.html(shuffle($container.children().get()));

function shuffle(o){
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

Shuffle function found here

Updated: jsFiddle

This example assumes you have to work with elements already on a page with classes assigned to them:

var classes = [];

// Populate classes array
// e.g., ["yellow", "red", "green", "blue", "pink", "orange", "black", "white"]
$('div.container').children().each(function (i, v) {
    classes.push(v.className);
});

// Assign random color to each div
$('div.container').children().each(function (i, v) {
    var color = Math.floor(Math.random()*classes.length)
    $(v).css({backgroundColor: classes.splice(color,1)});
});

http://jsfiddle.net/UgX3u/40/

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far