1

HTML 5 プレーヤーのシャッフル ボタンを作成しようとしています。私のプレーヤーは、次のトラックに移動するために次のように設定されています。

    $('.nextbutton').on("click", function(){
    var next = $('li.playing').next();
        if (!next.length) next = $('#stuff ul li').first();
        next.addClass('playing').siblings().removeClass('playing');
        var title = $('a#tunes img', next).attr("title");
            $("a.songtitle span").text(title);
        var artist = $('a#tunes img', next).attr("artist");
            $("a.songartist span").text(artist);
        audio.load($('a#tunes', next).attr('data-src'));
        audio.play();
    });

このコードは次の曲に移動するように機能しますが、これを変更して ?<li>の代わりにランダムな要素を選択するにはどうすればよい.next(); <li>ですか? お時間をいただきありがとうございます。あなたが私を助けてくれることを願っています!

4

2 に答える 2

3
var next = Math.floor(Math.random() * jQuery('#stuff ul li').length + 1));
var nextLi = jQuery('#stuff ul li').get(next);

li0 からs の数までの乱数を取得してから、 this を取得しliます。

于 2013-10-14T01:51:33.690 に答える
1

次のような乱数ジェネレーターを使用できます

var rand =  Math.floor(Math.random() * jQuery('#stuff ul li').length + 1);

また

var rand =  Math.ceil(Math.random() *( jQuery('#stuff ul li').length + 1));

リストでランダムな曲番号を取得し、次のように対応するアイテムを取得します。

var next = $('#stuff ul li').get(rand);
于 2013-10-14T02:08:47.297 に答える