选择随机< li>元件 [英] Select random <li> element

查看:91
本文介绍了选择随机< li>元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的HTML 5播放器制作一个随机播放按钮。我的播放器设置如下,以便进入下一首曲目:

I'm trying to make a shuffle button for my HTML 5 player. 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();
    });

此代码可用于转到下一首歌曲,但如何更改此代码以使其选择随机< li> 元素而不是 .next(); < li> ; ?感谢您的时间和考虑。希望你能帮助我!

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

它将获得0到 li s然后得到 li

It'll get random number between 0 and count of lis and then get this li.

这篇关于选择随机&lt; li&gt;元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆