选择随机元素

我正在尝试为我的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(); }); 

这段代码可以转到下一首歌,但是如何更改它以使其选择随机

  • 元素而不是.next();
  • ? 感谢您的时间和考虑。 希望你能帮我!

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

    它会得到介于0和li之间的随机数,然后得到这个li

    你可以使用随机数生成器

     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);