[英]Select random <li> element
I'm trying to make a shuffle button for my HTML 5 player. 我正在尝试为我的HTML 5播放器制作一个随机播放按钮。 My player is set up like this to go the next track: 我的播放器设置如下,以便进入下一首曲目:
$('.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();
});
This code works to go to the next song but how can I change this to make it so it selects a random <li>
element instead of the .next();
这段代码可以转到下一首歌,但是如何更改它以使其选择随机<li>
元素而不是.next();
<li>
? <li>
? Thanks for your time and consideration. 感谢您的时间和考虑。 Hope you can help me! 希望你能帮我!
var next = Math.floor(Math.random() * jQuery('#stuff ul li').length + 1));
var nextLi = jQuery('#stuff ul li').get(next);
It'll get random number between 0 and count of li
s and then get this li
. 它会得到介于0和li
之间的随机数,然后得到这个li
。
you can use a random number generator like 你可以使用随机数生成器
var rand = Math.floor(Math.random() * jQuery('#stuff ul li').length + 1);
or 要么
var rand = Math.ceil(Math.random() *( jQuery('#stuff ul li').length + 1));
for getting a random song number in the list , and then get the corresponding item like this: 获取列表中的随机歌曲编号,然后获取相应的项目,如下所示:
var next = $('#stuff ul li').get(rand);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.