Any way to shuffle content in multiple div elements

前端 未结 8 1117
鱼传尺愫
鱼传尺愫 2020-12-31 23:32

I\'m relatively new to Javascript and was wondering if there\'s a quick way to shuffle content that is contained in multiple

tags. For example

8条回答
  •  不思量自难忘°
    2021-01-01 00:19

    are you ok with using a javascript library like jQuery? here's a quick jQuery example to accomplish what you're after. the only modification to your HTML is the addition of a container element as suggested:

    ...
    ...
    ...

    and javascript:

    function shuffle(e) {               // pass the divs to the function
        var replace = $('
    '); var size = e.size(); while (size >= 1) { var rand = Math.floor(Math.random() * size); var temp = e.get(rand); // grab a random div from our set replace.append(temp); // add the selected div to our new set e = e.not(temp); // remove our selected div from the main set size--; } $('#shuffle').html(replace.html() ); // update our container div with the // new, randomized divs } shuffle( $('#shuffle div') );

提交回复
热议问题