[英]Select random <li> element
我正在嘗試為我的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>
? 感謝您的時間和考慮。 希望你能幫我!
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.