LoginSignup
0
0

More than 3 years have passed since last update.

javascriptでitemの件数を指定し、シャッフルする関数

Posted at

忘備録です。
既に設置してあるliが多い時、無理やり件数を絞ってシャッフル表示がしたい時用です。

var shuffleItem = function (max) {
   var randomContent = [];
    i = 0;
    $('.itemList .item').each(function() {
        randomContent.push($(this).html());
    });

    randomContent.sort(function() {
        return Math.random() - Math.random();
    });

    $('.itemList .item').remove();

    for (i = 0; i < max; i++) {
        $('ul.itemList').append('<li class="item">' + randomContent[i] + '</li>');
    }
}

よりスマートな書き方があれば教えてください。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0